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.

460 Exception Handling Chapter 11<br />

Testing and Debugging Tip 11.7<br />

A stack trace shows the complete method-call stack at the time an exception occurred. This<br />

enables the programmer <strong>to</strong> view the series of method calls that led <strong>to</strong> the exception. Information<br />

in the stack trace includes the names of the methods on the call stack at the time of the<br />

exception, names of the classes in which those methods are defined, names of the namespaces<br />

in which those classes are defined. The stack trace also includes line numbers; the first line<br />

number indicates the throw point, and subsequent line numbers indicate the locations from<br />

which the methods in the stack trace were called. 11.7<br />

Another property used frequently by class-library programmers is InnerException.<br />

Typically, programmers use this property <strong>to</strong> “wrap” exception objects caught in<br />

their code so that they then can throw new exception types that are specific <strong>to</strong> their libraries.<br />

For example, a programmer implementing an accounting system might have some accountnumber<br />

processing code in which account numbers are input as Strings, but represented<br />

as Integers in the code. Recall, a program can convert Strings <strong>to</strong> Integer values<br />

with Convert.ToInt32, which throws a FormatException when it encounters an<br />

invalid number format. When an invalid account-number format occurs, the accountingsystem<br />

programmer might wish employ a different error message than the default message<br />

supplied by FormatException or might wish <strong>to</strong> indicate a new exception type, such as<br />

InvalidAccountNumberFormatException. In these cases, the programmer<br />

would provide code <strong>to</strong> catch the FormatException and then would create an Exception<br />

object in the Catch handler, passing the original exception as one of the construc<strong>to</strong>r<br />

arguments. The original exception object becomes the InnerException of the new<br />

exception object. When an InvalidAccountNumberFormatException occurs in<br />

code that uses the accounting-system library, the Catch block that catches the exception<br />

can obtain a reference <strong>to</strong> the original exception via property InnerException. Thus,<br />

the exception indicates both that the user specified an invalid account number and that the<br />

particular problem was an invalid number format.<br />

Class Exception provides other properties, including HelpLink, Source and<br />

TargetSite. Property HelpLink specifies the location of the help file that describes<br />

the problem that occurred. This property is Nothing if no such file exists. Property<br />

Source specifies the name of the application where the exception occurred. Property<br />

TargetSite specifies the method where the exception originated.<br />

Our next example (Fig. 11.3) demonstrates properties Message, StackTrace and<br />

InnerException and method ToString of class Exception. In addition, this<br />

example introduces stack unwinding, which is the process of attempting <strong>to</strong> locate an appropriate<br />

Catch handler for an uncaught exception. As we discuss this example, we keep<br />

track of the methods on the call stack so that we can discuss property StackTrace and<br />

the stack-unwinding mechanism.<br />

<strong>Program</strong> execution begins with the invocation of Main, which becomes the first<br />

method on the method call stack. Line 13 of the Try block in Main invokes Method1<br />

(defined in lines 37–39), which becomes the second method on the stack. If Method1<br />

throws an exception, the Catch handler in lines 17–30 handles the exception and outputs<br />

information about the exception that occurred. Line 38 of Method1 invokes Method2<br />

(lines 42–44), which becomes the third method on the stack. Then, line 43 of Method2<br />

invokes Method3 (lines 47–61) which becomes the fourth method on the stack.

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

Saved successfully!

Ooh no, something went wrong!