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.]
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 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 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 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. |
Divide By Zero Without Exception Handling




