19.2. Example - Factorials

 
[Page 578 ( continued )]

17.2. Exception-Handling Overview

An exception is a runtime error. A program that does not provide code for catching and handling exceptions will terminate abnormally, and may cause serious problems. For example, if your program attempts to transfer money from a savings account to a checking account, but because of a runtime error is terminated after the money is drawn from the savings account and before the money is deposited in the checking account, the customer will lose money.

Exceptions occur for various reasons. The user may enter an invalid input, for example, or the program may attempt to open a file that doesn't exist, or the network connection may hang up, or the program may attempt to access an out-of-bounds array element.

You have already been briefly introduced to exceptions in earlier chapters. In Chapter 6, "Arrays," you learned that an ArrayIndexOutOfBoundsException occurs if you access an element past the end of an array. In Chapter 7, "Objects and Classes," you learned that a NullPointerException occurs when you invoke a method on a reference variable with null value. In Chapter 8, "Strings and Text I/O," you learned that a StringIndexOutOfBoundsException occurs when you access a character in a string using an out-of-bound index. You also learned that text I/O operations may throw exceptions.

When an exception occurs, the normal execution flow of the program will be interrupted . Java provides programmers with the capability to handle runtime exceptions. With this capability, referred to as exception handling , you can develop robust programs for mission-critical computing.

Here is an example. The program in Listing 17.1 terminates abnormally if you enter a floating-point value instead of an integer, as shown in Figure 17.1.

Listing 17.1. ExceptionDemo.java


[Page 579]
Figure 17.1. An exception occurs when you enter an invalid input.

Note that several lines of information are displayed in Figure 17.1 in response to this invalid input. This information, known as the stack trace , includes the name of the exception ( java.util.InputMismatchException ) followed by the method call stack at the time the exception occurred. The stack trace helps in debugging a program. Starting from the last line of the stack trace, you see that the exception was detected in line 7 of the main method. Each line of the stack trace contains the class name and method ( ExceptionDemo.main ) along with the file name and line number ( ExceptionDemo.java: 7 ). Moving up the stack trace, you see that the exception occurred in line 2000 in the nextInt method of the Scanner class, the exception occurred in line 2040 in the overloaded nextInt method of the Scanner class, the exception occurred in line 1431 in the next method of the Scanner class, the exception occurred in line 819 in the throwFor method of the Scanner class. The last method in the call chain actually threw an InputMismatchException .

Java allows the program to catch and process exceptions. You can revise Listing 17.1 to handle the InputMismatchException using a new construct called the try-catch block , as shown in Listing 17.2.

Listing 17.2. HandleExceptionDemo.java


[Page 580]

A try block begins with the keyword try followed by a block of statements in curly braces ( {} ). A try block contains the statements that might throw exceptions. A catch block begins with the keyword catch followed by an exception parameter in parentheses and a block of statements for handling the exception in curly braces. When a statement in a try block throws an exception, the rest of the statements in the try block are skipped and control is transferred to the catch block.

Suppose you enter 3.5 when executing line 11, as shown in Figure 17.2. An InputMismatchException occurs and the control is transferred to the catch block. The statements in the catch block are now executed. The statement scanner.nextLine() in line 22 discards the current input line so that the user can enter a new line of input. The variable continueInput controls the loop. Its initial value is true (line 6), and it is changed to false (line 17) when a valid input is received.

Figure 17.2. An exception is caught and handled in the program.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net