Obtaining Exception Information


Once you catch an exception, you may want more information about the error. Exception classes may contain proprietary information (as you will see in the section "Declaring Your Own Exception" later in this chapter), but since all exceptions derive from System.Exception there are a few pieces of information that all exception objects must have.

To extract exception information:

  1. First trap the exception with a try/catch block (see "Catching Exceptions" above).

  2. The catch block must catch a particular type of exception and use a variable to hold the exception object (see "Catching Specific Exceptions" earlier in this chapter).

  3. Inside the catch block type the name of the variable that holds a reference to the exception object and use one of the properties in Table 11.1 .

graphics/tick.gif Tips

  • If you look at Table 11.1 you will find a property called TargetSite. This property provides information about the method that generated the error. You can use a functionality known as reflection to extract information from this object such as the class name that contains the method, the method name, and even the method parameters. You'll learn about reflection in Chapter 12, "Reflection and Attributes." You can use e.TargetSite.Name to obtain the name of the method that produced the error, e.TargetSite.DeclaringType.FullName to obtain the name of the class that contains the method, and e.Targetsite.DeclaringType.Assembly.Location to get the path and name of the DLL that generated the error.

  • It's usually not a good idea to pass the exception information directly to the client. In other words, don't simply write Response.Write(ex.Message) . Error information is often meaningless to the client, and error information can provide hackers with information on how to exploit your system.


Table 11.1. System.Exception Properties and Methods (Properties and Methods that Enable You to Get More Information About the Exception)

PROPERTY

EXPLANATION

TYPE

HelpLink

A URL to a page or file that provides more information about the error.

Property string read/write

InnerException

Sometimes a function needs to report more than one exception, and may choose to create an exception chain.This property returns the next exception object in the chain.

Property System.Exception readonly

Message

A brief description of the error.

Property string readonly

Source

Describes the origin of the error. It could be the application name plus the class name plus the method that triggered the error, for example, Banking- Checking- MakeDeposit.

Property string read/write

StackTrace

A list of all the functions that were executing when the error occurred. The StackTrace reports something along the lines of "function a called function b which called function c and then an error occurred in line 5."

Property string readonly

TargetSite

An object that represents the method that triggered the error. You can use this object to retrieve information about the method, such as the method name, and parameter types.

Property System.Reflection.MethodBase readonly

GetBaseException

If there is a chain of exceptions, this method returns the root exception in the chain.

Method System.Exception

ToString

Returns information about the exception. Usually this information includes the name of the exception class, the exception Message, plus the StackTrace information.

Method string



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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