6.3 Displaying Exception Information

 <  Day Day Up  >  

You want to display information about an exception to aid in tracking down why it occurs.


Technique

Every exception that you can catch is derived from the System.Exception class. Within that class are several properties that you can access to acquire more information. The most frequently used property is Message , which returns a readable string detailing the error. Additional properties include Source , which displays the name of the application where the exception occurred, and TargetSite , which displays the name of the method that was currently executing when the exception was thrown.

To view the methods currently in scope, you can view the call stack by accessing the StackTrace string. Finally, if you want to determine whether an exception was thrown by an application, catch the ApplicationException exception object, and if you want to determine whether the exception being thrown is from the CLR, catch the SystemException object. Listing 6.3 demonstrates how to display exception information by throwing a variety of exceptions based on a user 's input.

Listing 6.3 Displaying Exception Information
 using System; namespace _3_ExceptionInfo {     class Class1     {         // outputs information about an exception         static void DisplayExceptionInformation( Exception e )         {             Console.WriteLine( "Application: {0}", e.Source );             Console.WriteLine( "Method: {0}", e.TargetSite );             Console.WriteLine( "Message: {0}", e.Message );             Console.WriteLine( "Call Stack: {0}", e.StackTrace );             if( e.HelpLink != null )                 Console.WriteLine( "Help Link: {0}", e.HelpLink );         }         // displays a menu and asks user for input         static int DisplayMenu()         {             int input = 0;             Console.WriteLine( "1) Throw a system exception" );             Console.WriteLine( "2) Throw an application exception" );             Console.WriteLine( "3) Quit" );             Console.Write( "Enter command: " );             try             {                 // get input from user                 input = Int32.Parse( Console.ReadLine() );             }             catch             {                 Console.WriteLine( "Invalid Input. Try again.\n" );                 return DisplayMenu();             }             return input;         }         [STAThread]         static void Main(string[] args)         {             int input = DisplayMenu();             while( input != 3 )             {                 // throw exception and display exception information                 try                 {                     switch( input )                     {                         case 1:                         {                             throw new SystemException();                         }                         case 2:                         {                             throw new ApplicationException();                         }                         default:                     }                 }                 catch( ApplicationException e )                 {                     Console.WriteLine( "An application exception occurred:" );                     DisplayExceptionInformation( e );                 }                 catch( SystemException e )                 {                     Console.WriteLine( "A system exception occurred:" );                     DisplayExceptionInformation( e );                 }                 catch( Exception e )                 {                     Console.WriteLine( "An unknown error occurred:" );                     DisplayExceptionInformation( e );                 }                 input = DisplayMenu();             }         }     } } 

Comments

One of the primary purposes of exceptions, other than preventing an application from prematurely exiting, is providing rich information about the error. Compare this information to the generic "This application has performed an illegal operation," followed by a list of the values currently in the processor registers and a hexadecimal dump of the current application stack ”a situation so often experienced with applications built without the exception support of the .NET Framework.

All of the exceptions within the .NET Framework are ultimately derived from the System.Exception class. In fact, most exceptions only override the Exception properties without defining additional properties or methods of their own. You can create a simple method that accepts an Exception object and rest assured that it will work with any exception passed to it. Additionally, you can use the class hierarchy of exceptions to your advantage to determine whether the exception is being thrown from an application or from the runtime itself. Listing 6.3 filters the exceptions by creating a catch block to catch any ApplicationException exceptions and any SystemException s. When an exception is thrown, the runtime accesses the application's exception-handling table to determine the correct catch handler. It does so by looking for a catch handler for the exact type of the exception. If it doesn't find one, the runtime then looks for a catch handler for that exception's base class. This process continues until the runtime finds a catch handler containing a base class of the exception object. The result of running the application in Listing 6.3 appears in Figure 6.2.

Figure 6.2. You can use the System.Exception class to display information about an exception when it occurs.

graphics/06fig02.jpg

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net