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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11 Exception Handling 471<br />

<strong>Visual</strong> <strong>Basic</strong> uses the termination model of exception handling. If an exception occurs in a Try<br />

block, the block expires and program control transfers <strong>to</strong> the first Catch handler following the<br />

Try block.<br />

The CLR searches for the first Catch handler that can process the type of exception that occurred.<br />

The appropriate handler is the first one in which the thrown exception’s type matches, or is derived<br />

from, the exception type specified by the Catch block’s exception parameter.<br />

If no exceptions occur in a Try block, the CLR ignores the exception handlers for that block.<br />

If no exception occurs or an exception is caught and handled, the program resumes execution with<br />

the next statement after the Try/Catch/Finally sequence.<br />

If an exception occurs in a statement that is not in a Try block, the method containing that statement<br />

terminates immediately, and the CLR attempts <strong>to</strong> locate an enclosing Try block in a calling<br />

method—a process called stack unwinding.<br />

When a Try block terminates, local variables defined in the block go out of scope.<br />

If an argument passed <strong>to</strong> method Convert.ToInt32 is not an Integer, a FormatException<br />

occurs.<br />

In integer arithmetic, an attempt <strong>to</strong> divide by zero causes a DivideByZeroException.<br />

A Try block encloses a portion of code that might throw exceptions, as well as any code that<br />

should not execute if an exception occurs.<br />

Each Catch handler begins with keyword Catch, followed by an optional exception parameter<br />

that specifies the type of exception handled by the Catch handler. The exception-handling code<br />

appears in the body of the Catch handler.<br />

If an exception occurs, the program executes only the matching Catch handler. When program<br />

control reaches the end of a Catch handler, the CLR considers the exception <strong>to</strong> be handled, and<br />

program control continues with the first statement after the Try/Catch sequence.<br />

The exception-handling mechanism allows only objects of class Exception and its derived<br />

classes <strong>to</strong> be thrown and caught. Class Exception of namespace System is the base class of<br />

the .<strong>NET</strong> Framework exception hierarchy.<br />

ApplicationException is a base class that programmers can extend <strong>to</strong> create exception data<br />

types that are specific <strong>to</strong> their applications. <strong>Program</strong>s can recover from most ApplicationExceptions<br />

and continue execution.<br />

The CLR generates SystemExceptions. If a program attempts <strong>to</strong> access an out-of-range array<br />

subscript, the CLR throws an IndexOutOfRangeException. An attempt <strong>to</strong> manipulate an<br />

object through a Nothing reference causes a NullReferenceException.<br />

<strong>Program</strong>s typically cannot recover from most exceptions thrown by the CLR. <strong>Program</strong>s generally<br />

should not throw SystemExceptions nor attempt <strong>to</strong> catch them.<br />

A Catch handler can catch exceptions of a particular type or can use a base-class type <strong>to</strong> catch<br />

exceptions in a hierarchy of related exception types. A Catch handler that specifies an exception<br />

parameter of type Exception can catch all exceptions, because Exception is the base class<br />

of all exception classes.<br />

For methods in the .<strong>NET</strong> Framework classes, programmers should investigate the detailed description<br />

of the method in the online documentation <strong>to</strong> determine whether the method throws exceptions.<br />

Information on exceptions thrown by the CLR appears in the <strong>Visual</strong> <strong>Basic</strong> Language Specification,<br />

which is located in the online documentation.<br />

Many computer operating systems prevent more than one program from manipulating a resource<br />

at the same time. Therefore, when a program no longer needs a resource, the program normally

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

Saved successfully!

Ooh no, something went wrong!