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

CHAPTER 11 EXCEPTIONS

The catch Clauses Section

The purpose of a catch clause is to allow you to handle an exception in an elegant way. If your catch clause is of the form that takes a parameter, then the system has set that exception variable to a reference to the exception object, which you can inspect to determine the cause of the exception. If the exception was the result of a previous exception, you can get a reference to that previous exception’s exception object from the exception variable’s InnerException property.

The catch clauses section can contain multiple catch clauses. Figure 11-4 shows a summary of the catch clauses section.

Figure 11-4. Structure of the catch clauses section of a try statement

When an exception is raised, the system searches the list of catch clauses in order, and the first catch clause that matches the type of the exception object is executed. Because of this, there are two important rules in ordering the catch clauses. They are the following:

The specific catch clauses must be ordered with the most specific exception types first, progressing to the most general. For example, if you declare an exception class derived from NullReferenceException, the catch clause for your derived exception type should be listed before the catch clause for NullReferenceException.

If there is a general catch clause, it must be last, after all specific catch clauses. Using the general catch clause is discouraged. You should use one of the specific catch clauses if at all possible. The general catch clause hides bugs by allowing the program to continue execution and can leave the program in an unknown state.

305

CHAPTER 11 EXCEPTIONS

The finally Block

If a program’s flow of control enters a try statement that has a finally block, the finally block is always executed. Figure 11-5 shows the flow of control.

If no exception occurs inside the try block, then at the end of the try block, control skips over any catch clauses and goes to the finally block.

If an exception occurs inside the try block, then the appropriate catch clause in the catch clauses section is executed, followed by execution of the finally block.

Figure 11-5. Execution of the finally block

The finally block will always be executed before returning to the calling code, even if a try block has a return statement or an exception is thrown in the catch block. For example, in the following code, there is a return statement in the middle of the try block that is executed under certain conditions. This does not allow it to bypass the finally statement.

try

{

if (inVal < 10) { Console.Write("First Branch - "); return;

}

else

Console.Write("Second Branch - ");

}

finally

{ Console.WriteLine("In finally statement"); }

This code produces the following output when variable inVal has the value 5:

First Branch - In finally statement

306

CHAPTER 11 EXCEPTIONS

Finding a Handler for an Exception

When a program raises an exception, the system checks to see whether the program has provided a handler for it. Figure 11-6 shows the flow of control.

If the exception occurred inside a try block, the system will check to see whether any of the catch clauses can handle the exception.

If an appropriate catch clause is found, the one of the following happens:

The catch clause is executed.

If there is a finally block, it is executed.

Execution continues after the end of the try statement (that is, after the finally block, or after the last catch clause if there is no finally block).

Figure 11-6. Exception with handler in current try statement

307

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