How nice would it be to have an object that can take an exception object and enumerate every InnerException message nested within? This could be very useful whenever an exception object has nested within it other exception objects, and would allow you, as the developer, to see the root cause exception’s string message easily.

Now you can, with a simple Recursive Exception Enumerator (formally called ExceptionDisplayer). This class, created in VB.NET in .NET 4.0, is very easy to use; simply instantiate this object with an exception as the argument. The following methods and properties will become available to you:

OrderOfExceptionMessages: This is an exposed enumeration of “OuterToInner” and “InnerToOuter”, which is described in the next property.

MessageSortOrder: This is a read/write property based on the above enumeration and determines the order in which the InnerException messages appear. If from OuterToInner, the most outer exception’s “InnerException” message appears first. Conversely, if MessageSortOrder is set to InnerToOuter, then the innermost exception’s InnerException will be returned first.

CountExceptionMessages: This determines how many exception messages are included and is based on how many exceptions are nested within the parent exception, plus the parent exception itself.

ExceptionMessages: This is an array of strings sorted by MessageSortOrder containing each InnerException message.

Now your challenge is to create an ExceptionMessage property that will iterate through all the available exceptions and return a string for each one, instead of an array of strings that ExceptionMessages returns.



Like this post? Share it!