Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

In Part 1 of this tutorial we showed the premature termination of a program in which unhandled exceptions occur. In this tutorial, we demonstrate how to catch and handle these exceptions to enable the program to continue executing. This tutorial is intended for students and professionals who are familiar with classes and basic inheritance concepts in Java.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 13.4) of Chapter 13, Exception Handling, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.643-648. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

13.4 Handling ArithmeticExceptions and InputMismatchExceptions (Continued)
Termination Model of Exception Handling

If an exception occurs in a try block (such as an InputMismatchException being thrown as a result of the code at line 25 of Fig. Fig. 13.2), the try block terminates immediately and program control transfers to the first of the following catch blocks in which the exception parameter’s type matches the type of the thrown exception. In Fig. Fig. 13.2, the first catch block catches InputMismatchExceptions (which occur if invalid input is entered) and the second catch block catches ArithmeticExceptions (which occur if an attempt is made to divide by zero). After the exception is handled, program control does not return to the throw point because the try block has expired (which also causes any of its local variables to be lost). Rather control resumes after the last catch block. This is known as the termination model of exception handling. [Note: Some languages use the resumption model of exception handling, in which, after an exception is handled, control resumes just after the throw point.]

Common Programming Error
Common Programming Error 13.4
Logic errors can occur if you assume that after an exception is handled, control will return to the first statement after the throw point.

Common Programming Error
Common Programming Error 13.4
Logic errors can occur if you assume that after an exception is handled, control will return to the first statement after the throw point.
Error-Prevention Tip
Error-Prevention Tip 13.2
With exception handling, a program can continue executing (rather than terminating) after dealing with a problem. This helps ensure the kind of robust applications that contribute to what is called mission-critical computing or business-critical computing.
Error-Prevention Tip
Error-Prevention Tip 13.2
With exception handling, a program can continue executing (rather than terminating) after dealing with a problem. This helps ensure the kind of robust applications that contribute to what is called mission-critical computing or business-critical computing.

    Notice that we name our exception parameters (inputMismatchException and arithmeticException) based on their type. Java programmers often simply use the letter e as the name of their exception parameters.

Good Programming Practice
Good Programming Practice 13.1
Using an exception parameter name that reflects the parameter’s type promotes clarity by reminding the programmer of the type of exception being handled.
After executing a catch block, this program’s flow of control proceeds to the first statement after the last catch block (line 48 in this case). The condition in the do...while statement is true (variable continueLoop contains its initial value of true), so control returns to the beginning of the loop and the user is once again prompted for input. This control statement will loop until valid input is entered. At that point, program control reaches line 32 which assigns false to variable continueLoop. The try block then terminates. If no exceptions are thrown in the try block, the catch blocks are skipped and control continues with the first statement after the catch blocks (or after the finally block, if one is present). Now the condition for the do...while loop is false, and method main ends. The try block and its corresponding catch and/or finally blocks together form a try statement. It is important not to confuse the terms “try block” and “try statement”—the term “try block”refers to the keyword try followed by a block of code, while the term “try statement” includes the try block, as well as the following catch blocks and/or finally block. As with any other block of code, when a try block terminates, local variables declared in the block go out of scope (and are destroyed). When a catch block terminates, local variables declared within the catch block (including the exception parameter of that catch block) also go out of scope and are destroyed. Any remaining catch blocks in the try statement are ignored and execution resumes at the first line of code after the trycatch sequencethis will be a finally block, if one is present. Using the throws Clause
Page 1 | 2 | 3 | 4
Related Tutorials :
Divide By Zero Without Exception Handling
Return to Tutorial Index

Google