Home of the AlmostImplementedException

MethodBase – How to get more information from an exception

Sometimes you want to know where an exception is comming from, especially when you use a central error logging. For this the Exception-Class has a readonly property called TargetSite which returns a MethodBase-Class. This class is part of System.Reflection and gives you detailed information about a method.
You can see if this method is public, private or event static and much more. To much to present them all, so i will stick to 4 important:

  • Name is the name of the method
  • DeclaringType is the class where the method is declared
  • MemberType is the type of the code (a method, constructor, event, field …)
  • Module is the name of the dll/executable

But enought words, here is an example

As you can see, the Main method is calling DoSomething which is producing an exception for us. Then this exception is catched and TargetSite presents us with aditional information.

Thats it.

Share :

, , ,

Leave a Reply