Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 11 EXCEPTIONS

The Exception Classes

There are many different types of exceptions that can occur in a program. The BCL defines a number of exception classes, each representing a specific type. When one occurs, the CLR does the following:

It creates an exception object for the type.

It looks for an appropriate catch clause to handle it.

All exception classes are ultimately derived from the System.Exception class. Figure 11-2 shows a portion of the exception inheritance hierarchy.

Figure 11-2. Structure of the exception hierarchy

301

CHAPTER 11 EXCEPTIONS

An exception object contains read-only properties with information about the exception that caused it. Table 11-1 shows some of these properties.

Table 11-1. Selected Properties of an Exception Object

Property

Type

Description

Message

string

This property contains an error message explaining the cause of the

 

 

exception.

StackTrace

string

This property contains information describing where the exception

 

 

occurred.

InnerException

Exception

If the current exception was raised by another exception, this property

 

 

contains a reference to the previous exception.

HelpLink

string

This property can be set by application-defined exceptions to give a

 

 

URN or URL for information on the cause of the exception.

Source

string

If not set by an application-defined exception, this property contains

 

 

the name of the assembly where the exception originated.

302

CHAPTER 11 EXCEPTIONS

The catch Clause

The catch clause handles exceptions. There are three forms, allowing different levels of processing. Figure 11-3 shows the forms.

Figure 11-3. The three forms of the catch clause

The general catch clause can accept any exception but can’t determine the type of exception that caused it. This allows only general processing and cleanup for whatever exception might occur.

The specific catch clause form takes the name of an exception class as a parameter. It matches exceptions of the specified class or exception classes derived from it.

The specific catch clause with object form gives you the most information about the exception. It matches exceptions of the specified class, or exception classes derived from it. It gives you a reference to the exception object created by the CLR, by assigning it to the exception variable. You can access the exception variable’s properties within the block of the catch clause to get specific information about the exception raised.

For example, the following code handles exceptions of type IndexOutOfRangeException. When one occurs, a reference to the actual exception object is passed into the code with parameter name e. The three WriteLine statements each read a string field from the exception object.

Exception type Exception variable

 

 

 

catch ( IndexOutOfRangeException

e )

 

 

{

 

Accessing the exception variable

 

 

 

 

Console.WriteLine( "Message: {0}",

e.Message );

Console.WriteLine( "Source: {0}",

e.Source );

Console.WriteLine( "Stack:

{0}",

e.StackTrace );

303

CHAPTER 11 EXCEPTIONS

Examples Using Specific catch Clauses

Going back to our divide-by-zero example, the following code modifies the previous catch clause to specifically handle exceptions of the DivideByZeroException class. While in the previous example, the catch clause would handle any exception raised in the try block, the current example will only handle those of the DivideByZeroException class.

int x = 10;

 

 

try

 

 

{

 

 

 

int y = 0;

 

 

x /= y;

 

// Raises an exception

}

 

Exception type

 

 

 

catch ( DivideByZeroException )

{

...

Console.WriteLine("Handling an exception.");

}

You could further modify the catch clause to use an exception variable. This allows you to access the exception object inside the catch block.

int x = 10;

 

 

 

 

 

 

try

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

int y = 0;

 

 

 

 

 

 

x /= y;

 

 

// Raises an exception

}

 

Exception type

Exception variable

 

 

 

 

catch ( DivideByZeroException

e )

{

 

 

 

 

Accessing the exception variable

 

 

 

 

 

 

 

Console.WriteLine("Message: {0}", e.Message );

Console.WriteLine("Source:

{0}", e.Source );

Console.WriteLine("Stack:

{0}", e.StackTrace );

}

 

 

 

 

 

 

 

 

This code produces the following output:

 

 

Message: Attempted to divide by zero.

Source:

Exceptions 1

 

 

 

 

 

 

Stack:

 

at Exceptions_1.Program.Main() in C:\Progs\Exceptions 1\

Exceptions 1\Program.cs:line 14

304

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]