System.Exception

.NET generates an exception object any time an unexpected condition is encountered. This enables a comprehensive, consistent approach to handling such conditions in any type of .NET module.

An exception object is an instance of a class that derives from a class named System.Exception. A variety of subclasses of System.Exception are available for different circumstances. These subclasses allow condition-specific information about the exception to be exposed.

The base Exception class has properties that contain useful information about typical exceptions, as shown in Tables 6.1 and 6.2.

Table 6.1 Exception Class Properties

Property Description
HelpLink A string indicating the link to help for this exception.
InnerException Returns the exception object reference to an inner (nested) exception.
Message A string that contains a description of the error, suitable for displaying to users.
Source The name of the object that generated the error.
StackTrace A read-only property. The stack trace is a list of the method calls at the point at which the exception was detected. That is, if MethodA called MethodB, and an exception occurred in MethodB, then the stack trace would contain both MethodA and MethodB.
TargetSite A read-only string property that holds the method that threw the exception.

Table 6.2 Exception Class Methods

Method Description
GetBaseException Returns the first exception in the chain
ToString Returns the error string, which might include as much information as the error message, the inner exceptions, and the stack trace, depending on the error

There are many types of exception objects in the .NET Framework that derive from the base Exception class. Each is customized for a particular type of exception. For example, if a divide by zero is done in code, then an OverflowException is generated.

Special-purpose exception classes can be found in many namespaces. It is common for an exception class to reside in a namespace with the classes that typically generate the exception. For example, the DataException class is in System.Data, with the ADO.NET components that often generate a DataException instance.

In addition to the dozens of exception types available in the .NET Framework, you can create your own classes that inherit from ApplicationException. This allows you to add custom properties and methods for passing key data related to unexpected events within your application. Of course the next step is to understand how you will reference this class within your applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset