Exceptions in .NET


.NET implements a systemwide, comprehensive approach to exception handling. As noted in the chapter introduction, instead of an error number, there is an exception object. This object contains information relevant to the error, exposed as properties of the object. Later you’ll see a summary of the properties and the information they expose in a table.

Such an object is an instance of a class that derives from a class named System.Exception. As shown later, a variety of subclasses of System.Exception are used for different circumstances.

Important Properties and Methods of an Exception

The Exception class has properties that contain useful information about the exception, as shown in the following table:

Open table as spreadsheet

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

A string containing the name of an object that generated the error

StackTrace

A read-only property that holds the stack trace as a text string. The stack trace is a list of the pending method calls at the point at which the exception was detected. That is, if MethodA called MethodB, and an exception occurred in MethodB, the stack trace would contain both MethodA and MethodB.

TargetSite

A read-only string property that holds the method that threw the exception

The two most important methods of the Exception class are as follows:

Open table as spreadsheet

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

You will see these properties and methods used in the code examples given later, once you have covered the syntax for detecting and handling exceptions.

How Exceptions Differ from the Err Object in VB6

Because an exception contains all of the information needed about an error, structured exception handling does not use error numbers and the Err object. The exception object contains all the relevant information about the error.

However, whereas there is only one global Err object in VB6, there are many types of exception objects in VB 2005. For example, if a divide by zero is done in code, then an OverflowException is generated. There are several dozen types of exception classes in VB 2005, and in addition to using the ones that are available in the .NET Framework, you can inherit from a class called ApplicationException and then create your own exception classes (see Chapter 4 for a discussion of inheritance).

In .NET, all exceptions inherit from System.Exception. Special-purpose exception classes can be found in many namespaces. The following table lists four representative examples of the classes that extend Exception:

Open table as spreadsheet

Namespace

Class

Description

System

InvalidOperationException

Generated when a call to an object method is inappropriate because of the object’s state

System

OutOfMemoryException

Results when there is not enough memory to carry out an operation

System.XML

XmlException

Often caused by an attempt to read invalid XML

System.Data

DataException

Represents errors in ADO.NET components

There are literally dozens of exception classes scattered throughout the .NET Framework 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.

Having many types of exceptions in VB 2005 enables different types of conditions to be trapped with different exception handlers. This is a major advance over VB6. The syntax to accomplish that is discussed next.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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