There are several different methods of displaying exception information. You need to choose the best one to use.
The .NET platform supports several methods for displaying exception
information, depending on the specific type of information that you
want to show. The easiest method is to use the
ToString
method
of the thrown exception object, usually in the
catch
block of an exception handler:
catch(Exception e) { Console.WriteLine(e.ToString( )); }
Another method is to manually display the individual properties of
the thrown exception and iterate through each inner exception, if any
exist. For example, the following custom method is called from a
catch
block that takes a single exception object
as a parameter and proceeds to display its information, including
information on all inner
exceptions:
public void DisplayException(Exception e) { Console.WriteLine("Outer Exception."); Console.WriteLine("ExceptionType: " + e.GetType( ).Name); Console.WriteLine("HelpLine: " + e.HelpLink); Console.WriteLine("Message: " + e.Message); Console.WriteLine("Source: " + e.Source); Console.WriteLine("StackTrace: " + e.StackTrace); Console.WriteLine("TargetSite: " + e.TargetSite); string indent = " "; Exception ie = e; while(ie.InnerException != null) { ie = ie.InnerException; Console.WriteLine("Inner Exception."); Console.WriteLine(indent + "ExceptionType: " + ie.GetType( ).Name); Console.WriteLine(indent + "HelpLink: " + ie.HelpLink); Console.WriteLine(indent + "Message: " + ie.Message); Console.WriteLine(indent + "Source: " + ie.Source); Console.WriteLine(indent + "StackTrace: " + ie.StackTrace); Console.WriteLine(indent + "TargetSite: " + ie.TargetSite); indent += " "; } }
A typical exception object of type
Exception
displays the following information if
its ToString
method is called:
System.Exception: Exception of type System.Exception was thrown. at Chapter_Code.Chapter7.TestSpecializedException( ) in c:ook cs cookbookcode test.cs:line 286
There are three pieces of information shown here:
The exception type (Exception
in this case)
followed by a colon.
The string contained in the exception’s
Message
property.
The string contained in the exception’s
StackTrace
property.
The great thing about the ToString
method is that
information about any exception contained in the
InnerException
property is automatically displayed
as well. The following text shows the output of an exception that
wraps an inner exception:
System.Exception: Exception of type System.Exception was thrown. ---> System.Exception: The Inner Exception at Chapter_Code.Chapter7.TestSpecializedException( ) in c:ook cs cookbookcode test.cs:line 306 --- End of inner exception stack trace --- at Chapter_Code.Chapter7.TestSpecializedException( ) in c:ook cs cookbookcode test.cs:line 310
The same three pieces of information are displayed for each exception. The output is broken down into the following format:
Outer exception type: Outer exception Message property ---> Inner Exception type: Inner exception Message property Inner Exception StackTrace property --- End of inner exception stack trace --- Outer exception StackTrace property
If the inner exception contains an exception object in its
InnerException
property, that exception is
displayed as well. In fact, information for all inner exceptions is
displayed in this format.
Calling the ToString
method is a quick, useful way
of getting the most pertinent information out of the exception and
displaying it in a formatted string. However, not all of the
exception’s information is displayed. There might be
a need to display the HelpLine
or
Source
properties of the exception. In fact, if
this is a user-defined exception, there could be custom fields that
need to be displayed or captured in an error log. Also, you might not
like the default formatting that the ToString
method offers. In these cases, consider writing your own method to
display the exception’s information.
To illustrate the custom method presented in the Solution section
(the DisplayException
method), consider the
following code, which throws an exception that wraps two inner
exceptions:
Exception innerInner = new Exception("The innerInner Exception."); ArgumentException inner = new ArgumentException("The inner Exception.", innerInner); NullReferenceException se = new NullReferenceException("A Test Message.", inner); se.HelpLink = "MyComponent.hlp"; se.Source = "MyComponent"; try { throw (se); } catch(Exception e) { DisplayException(e); }
If this code were executed, DisplayException
would
display the following:
Outer Exception. ExceptionType: NullReferenceException HelpLine: MyComponent.hlp Message: A Test Message. Source: MyComponent StackTrace: at Chapter_Code.SEH.DisplayException( ) in c:ook cs cookbookcode test.cs:line 219 TargetSite: Void DisplayException( ) inner Exception. ExceptionType: ArgumentException HelpLink: Message: The inner Exception. Source: StackTrace: TargetSite: inner Exception. ExceptionType: Exception HelpLink: Message: The innerInner Exception. Source: StackTrace: TargetSite:
The outermost exception is displayed first, followed by all of its properties. Next, each inner exception is displayed in a similar manner.
The while loop of the DisplayException
method is
used to iterate through each inner exception until the innermost
exception is reached. The indent
variable is used
to create the staggered display of inner exception information.
Initially, this variable contains a single tab character
('
'). A single tab character is added to this
variable at the end of each iteration of the loop, allowing for the
creation of the staggered display.