Structured Exception Handling in .NET Part 3: The Exception Class

This entry is part of a series, Structured Exception Handling in .NET»

The base .NET Exception class has several useful properties to retrieve data about what went happened: -

Message
This is probably the most used one.  It is simply a textual message explaining some detail about the exception.

InnerException
Sometimes you’ll find that some piece of code lower down the line has caught an exception and wanted to add some information itself, so what it does is to create a new Exception, and put the one that it caught into the InnerException property.  Sometimes you really need to access this in order to find out what happened, because the actual exception that you caught is very non-specific about what happened.  The other thing to bear in mind is that the InnerException can also have an InnerException and so on.

StackTrace
This returns a string that contains a stack trace that you can use to see how the code got to this point.  It can be very useful for debugging.

Source
This returns a string that tells you the name of what caused the error.  I usually don’t tend to find this particularly useful

TargetSite
This returns a reflection object representing the method that the exception occurred within.  I’ve not actually found any use for this yet, but I can see how it could be useful.

Data
This is a Dictionary of any extra pieces of information you might need to know about the Exception.

No related posts.

Tags: , ,

2 Responses to “Structured Exception Handling in .NET Part 3: The Exception Class”

  1. [...] about how the source code maps to the compiled code.  This means that eg. when you get an Exception, the StackTrace property will give you information including line [...]

  2. [...] something unexpected happened.  In order to throw an Exception, first you create an object of type Exception, or a class which inherits from Exception if there is a more appropriate one.  You should also set [...]

Leave a Reply