13. Exceptions: When Things Go Wrong

13.2. Handling Exceptional Conditions

To introduce you to handling exceptional conditions, Figure 10.1 shows a method that computes the average of the first N integers, an admit-

 

 

,,

 

 

 

 

 

 

 

J

Figure 10.1: Poor design. No attempt is made to guard against a divide- by-zero error.

 

SECTION 10.2 Handling Exceptional Conditions461

,,

 

 

 

 

 

 

 

 

 

 

J

Figure 10.2: One way to handle a divide-by-zero error might be to ter- minate the program, if there is an attempt to divide by 0, assuming it’s the kind of program that can be safely aborted. This version does not use exception handling.

 

 

tedly contrived example. We use it mainly to illustrate the basic con- cepts involved in exception handling. As its precondition suggests, the avgFirstN() method expects that N will be greater than 0. If N happens to be 0, an error will occur in the expression sum/N, because you cannot divide an integer by 0.

Traditional Error Handling

Obviously, the method in Figure 10.1 should not simply ignore the possi-

bility that N might be 0. Figure 10.2 shows a revised version of the method,Divide-by-zero error

which includes code that takes action if the method’s precondition fails. Because there is no way to compute an average of 0 elements, the revised method decides to abort the program. Aborting the program appears to be a better alternative than returning 0 or some other default value (like 1) as the method’s result and thereby allowing an erroneous value to

spread throughout the program. That would just compound the error.

 

The revised avgFirstN() method takes the traditional approach to er- ror handling: Error-handling code is built right into the algorithm. If N happens to be 0 when avgFirstN() is called, the following output will be generated:

,,

 

J

 

,,

 

 

 

 

 

 

 

 

 

 

 

J

Figure 10.3: Note that there are two public classes defined in this figure, which would be saved in separate Java files.

 

Java’s Default Exception Handling

To help detect and handle common runtime errors, Java’s creators incor- porated an exception-handling model into the language itself. In the case of our divide-by-zero error, the Java Virtual Machine (JVM) would detect the error and abort the program. To see this, consider the program in Fig- ure 10.3. Note that the avgFirstN() method is passed an argument of 0 in the CalcAvgTest.main(). When the JVM detects the error, it will abort the program and print the following message:

,,

 

 

 

J

The error message describes the error and provides a trace of the method calls, from last to first, that led to the error. This trace shows that the error occurred in the CalcAverage.avgFirstN() method, which was called by the CalcAvgTest.main() method.

As this example suggests, Java’s default exception handling is able to

detect and handle certain kinds of errors and exceptional conditions. In the next section, we will identify what kinds of conditions are handled by the JVM.