#885 – Getting Information about the Method that Threw an Exception

In an exception handler, you can use the TargetSite property of the Exception object to get information about the method that threw the exception.  This can be useful for diagnostics purposes, since you can get the name of the method, as well as the type and module where it was defined.

        static void Main(string[] args)
            Console.WriteLine("About to do some stuff");

            catch (Exception xx)
                Console.WriteLine("** Exception **");

                MethodBase originMethod = xx.TargetSite;

                Console.WriteLine("Exception origin info:");
                Console.WriteLine("  Method {0}", originMethod.Name);
                Console.WriteLine("  Defined in {0}", originMethod.DeclaringType);
                Console.WriteLine("  Type is {0}", originMethod.MemberType);
                Console.WriteLine("  Defined in {0}", originMethod.Module);


        static void DoSomeStuff()
            Dog d1 = new Dog("Jack", 15);



About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #885 – Getting Information about the Method that Threw an Exception

  1. Pingback: Dew Drop – July 11, 2013 (#1,583) | Alvin Ashcraft's Morning Dew

  2. Pingback: Energy Consumption Tool Featured In The Daily Six Pack: July 12, 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: