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.

 



”’ <summary>
”’ An object which can take an exception object and display all the inner messages as an array of strings
”’ </summary>
”’ <remarks></remarks>

Public Class ExceptionDisplayer
Dim __exceptionMessage As String()
Private __sortOrder As OrderOfExceptionMessages = OrderOfExceptionMessages.OuterToInner


”’ <summary>
”’ Displays or sets the sort order of exception messages.
”’ </summary>
”’ <value></value>
”’ <returns></returns>
”’ <remarks></remarks>

Public Property MessageSortOrder As OrderOfExceptionMessages
Get
Return __sortOrder
End Get
Set(value As OrderOfExceptionMessages)
Select Case value
Case OrderOfExceptionMessages.InnerToOuter, OrderOfExceptionMessages.OuterToInner
If __sortOrder <> value Then
__exceptionMessage.Reverse()
__sortOrder = value
End If
Case Else
Throw New ExceptionExceptionDisplayer(“MessageSortOrder property set to an invalid value”)
End Select
End Set
End Property


”’ <summary>
”’ Enumeration of possible values for property MessageSortOrder
”’ </summary>
”’ <remarks></remarks>

Public Enum OrderOfExceptionMessages
OuterToInner
InnerToOuter
End Enum


”’ <summary>
”’ Create a new exception display maker object.
”’ </summary>
”’ <param name=”ex”>Any exception object which may contain inner exceptions</param>
”’ <remarks></remarks>

Public Sub New(ex As Exception)
__exceptionMessage = GetExceptionMessage(ex, __exceptionMessage)
End Sub


”’ <summary>
”’ Array of strings, each string the message property of a nested exception
”’ </summary>
”’ <value></value>
”’ <returns></returns>
”’ <remarks></remarks>

Public ReadOnly Property ExceptionMessages() As String()
Get
Return __exceptionMessage
End Get
End Property


”’ <summary>
”’ Provides an integer count of exception messages available in property ExceptionMessage
”’ </summary>
”’ <value></value>
”’ <returns></returns>
”’ <remarks></remarks>

Public ReadOnly Property CountExceptionMessages As Integer
Get
If IsNothing(__exceptionMessage) Then
Return 0
Else
Return __exceptionMessage.Length
End If
End Get
End Property

Private Function GetExceptionMessage(ByRef ex As Exception, ByRef exceptionMessage As String()) As String()
Dim nextElement As Int16
If IsNothing(exceptionMessage) Then
nextElement = 0
Else
nextElement = exceptionMessage.Length
End If
ReDim Preserve exceptionMessage(nextElement)
exceptionMessage(nextElement) = ex.Message
If Not IsNothing(ex.InnerException) Then
GetExceptionMessage = GetExceptionMessage(ex.InnerException, exceptionMessage)
End If
Return exceptionMessage
End Function
End Class

Public Class ExceptionExceptionDisplayer
Inherits System.Exception

Protected Friend Sub New(message As String)
MyBase.New(message)
End Sub

Protected Friend Sub New(message As String, innerException As System.Exception)
MyBase.New(message, innerException)
End Sub
End Class

 

Need to test the height of this box....

Social Wall