30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

458 Exception Handling Chapter 11<br />

fore, the exception is not caught in method ThrowExceptionWithoutCatch. Normal<br />

program control cannot continue until the exception is caught and processed. Thus, the<br />

CLR terminates ThrowExceptionWithoutCatch, and program control returns <strong>to</strong><br />

Main. Before control returns <strong>to</strong> Main, the Finally block (lines 110–112) executes and<br />

outputs a message. At this point, program control returns <strong>to</strong> Main—any statements<br />

appearing after the Finally block (e.g., line 117) do not execute. In this example, such<br />

statements could cause logic errors, because the exception thrown in lines 106–107 is not<br />

caught. In Main, the Catch handler in lines 30–32 catches the exception and displays a<br />

message indicating that the exception was caught in Main.<br />

Common <strong>Program</strong>ming Error 11.6<br />

The argument of a Throw—an exception object—must be of class Exception or one of its<br />

derived classes. 11.6<br />

Lines 42–50 of Main define a Try block in which Main invokes method Throw-<br />

ExceptionCatchRethrow (lines 121–149). The Try block enables Main <strong>to</strong> catch<br />

any exceptions thrown by ThrowExceptionCatchRethrow. The Try block in lines<br />

124–145 of ThrowExceptionCatchRethrow begins by outputting a message. Next,<br />

the Try block throws an Exception (lines 127–128). The Try block expires immediately,<br />

and program control continues at the first Catch (lines 131–136) following the Try<br />

block. In this example, the type thrown (Exception) matches the type specified in the<br />

Catch, so lines 132–133 outputs a message indicating where the exception occurred. Line<br />

136 uses the Throw statement <strong>to</strong> rethrow the exception. This indicates that the Catch<br />

handler performed partial processing of the exception and now is passing the exception<br />

back <strong>to</strong> the calling method (in this case, Main) for further processing. Note that the argument<br />

<strong>to</strong> the Throw statement is the reference <strong>to</strong> the exception that was caught. When<br />

rethrowing the original exception, you also can use the statement<br />

Throw<br />

with no argument. Section 11.6 demonstrates using a Throw statement with an argument<br />

from a Catch handler. After an exception is caught, such a Throw statement enables programmers<br />

<strong>to</strong> create an exception object then throw a different type of exception from the<br />

Catch handler. Class-library designers often do this <strong>to</strong> cus<strong>to</strong>mize the exception types thrown<br />

from methods in their class libraries or <strong>to</strong> provide additional debugging information.<br />

Software Engineering Observation 11.7<br />

Before rethrowing an exception <strong>to</strong> a calling method, the method that rethrows the exception<br />

should release any resources it acquired before the exception occurred. 7<br />

11.7<br />

Software Engineering Observation 11.8<br />

Whenever possible, a method should handle exceptions that are thrown in that method, rather<br />

than passing the exceptions <strong>to</strong> another region of the program. 11.8<br />

The exception handling in method ThrowExceptionCatchRethrow does not<br />

complete, because the program cannot run code in the Catch handler placed after the<br />

invocation of the Throw statement (line 136). Therefore, method Throw-<br />

ExceptionCatchRethrow terminates and returns control <strong>to</strong> Main. Once again, the<br />

7. “Best Practices for Handling Exceptions [<strong>Visual</strong> <strong>Basic</strong>].”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!