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 449<br />

Now, we consider the user interactions and flow of control that yield the results shown<br />

in the sample output windows. The user inputs values in<strong>to</strong> the TextBoxes that represent<br />

the numera<strong>to</strong>r and denomina<strong>to</strong>r and then presses Click To Divide. At this point, the program<br />

invokes method cmdDivide_Click. Line 28 assigns the empty String <strong>to</strong><br />

lblOutput <strong>to</strong> clear any prior result, because the program is about <strong>to</strong> attempt a new calculation.<br />

Lines 31–59 define a Try block enclosing the code that might throw exceptions,<br />

as well as the code that should not execute if an exception occurs. For example, the program<br />

should not display a new result in lblOutput (line 45) unless the calculation (line 43)<br />

completes successfully. Remember that the Try block terminates immediately if an exception<br />

occurs, so the remaining code in the Try block will not execute.<br />

Software Engineering Observation 11.5<br />

Enclose in a Try block a significant logical section of the program in which several statements<br />

can throw exceptions, rather than using a separate Try block for every statement that<br />

might throw an exception. <strong>How</strong>ever, each Try block should enclose a small enough section<br />

of code such that when an exception occurs, the specific context is known, and the Catch<br />

handlers can process the exception properly. 11.5<br />

The two statements that read the Integers from the TextBoxes (lines 35–39) call<br />

method Convert.ToInt32 <strong>to</strong> convert Strings <strong>to</strong> Integer values. This method<br />

throws a FormatException if it cannot convert its String argument <strong>to</strong> an Integer.<br />

If lines 35–39 convert the values properly (i.e., no exceptions occur), then line 43 divides<br />

the numera<strong>to</strong>r by the denomina<strong>to</strong>r and assigns the result <strong>to</strong> variable result. If the<br />

denomina<strong>to</strong>r is zero, line 43 causes the CLR <strong>to</strong> throw a DivideByZeroException. If<br />

line 43 does not cause an exception <strong>to</strong> be thrown, then line 45 displays the result of the division.<br />

If no exceptions occur in the Try block, the program successfully completes the Try<br />

block by ignoring the Catch handlers at lines 48–51 and 54–57 and reaching line 59.<br />

Then, the program executes the first statement following the Try/Catch sequence. In this<br />

example, the program reaches the end of event handler cmdDivide_Click (line 61), so<br />

the method terminates, and the program awaits the next user interaction.<br />

Immediately following the Try block are two Catch handlers. Lines 48–51 define<br />

the Catch handler for a FormatException, and lines 54–57 define the Catch handler<br />

for the DivideByZeroException. Each Catch handler begins with keyword<br />

Catch, followed by an exception parameter that specifies the type of exception handled<br />

by the Catch block. The exception-handling code appears in the Catch-handler body. In<br />

general, when an exception occurs in a Try block, a Catch block catches the exception<br />

and handles it. In Fig. 11.1, the first Catch handler specifies that it catches FormatExceptions<br />

(thrown by method Convert.ToInt32), and the second Catch block specifies<br />

that it catches DivideByZeroExceptions (thrown by the CLR). If an exception<br />

occurs, the program executes only the matching Catch handler. Both the exception handlers<br />

in this example display an error message dialog. When program control reaches the<br />

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

control continues with the first statement after the Try/Catch sequence (the end of the<br />

method, in this example).<br />

In the second sample output, the user input hello as the denomina<strong>to</strong>r. When lines 38–<br />

39 execute, Convert.ToInt32 cannot convert this String <strong>to</strong> an Integer, so Convert.ToInt32<br />

creates a FormatException object and throws it <strong>to</strong> indicate that the<br />

method was unable <strong>to</strong> convert the String <strong>to</strong> an Integer. When the exception occurs,

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

Saved successfully!

Ooh no, something went wrong!