
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
394
|
Chapter 7: Exception Handling
is called from a catch block. It takes a single exception object as a parameter and
proceeds to display its information, including information on all inner exceptions
and the exception data block.
Discussion
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:\book cs cookbook\code\
test.cs:line 286
Example 7-3. Displaying exception information, including information on all inner exceptions and
the exception data block
public static int exceptionLevel = 0;
public static void DisplayException(Exception e)
{
// Increment exception level.
exceptionLevel++;
// Make spacer for level.
string indent = new string('\t',exceptionLevel-1);
// Write out exception level data.
Console.WriteLine(indent + "*** Exception Level {0} " +
"***************************************", exceptionLevel);
Console.WriteLine(indent + "ExceptionType: " + e.GetType( ).Name.ToString( ));
Console.WriteLine(indent + "HelpLine: " + e.HelpLink);
Console.WriteLine(indent + "Message: " + e.Message);
Console.WriteLine(indent + "Source: " + e.Source);
Console.WriteLine(indent + "StackTrace: " + e.StackTrace); ...