Java Reference
In-Depth Information
and rerun the program with the same input as in Sample Run 3, then this sample
run is:
Sample Run 3 (with the modified catch block):
Line 4: Enter the dividend: 2753
Line 7: Enter the divisor: 2f1
Line 17: Exception java.util.InputMismatchException 2f1
The modified program, named ExceptionExample3A.java ,canbefoundwiththe
Additional Student Files at www.cengagebrain.com.
As noted, when an exception occurs in a program, the program throws an object of a
specific exception class. Example 11-3 illustrates how to handle an arithmetic exception
and an input mismatch exception. We also noted that the class Exception (directly or
indirectly) becomes the superclass of the exception classes. Because Java provides many
classes for handling exceptions, before giving more examples of exception handling, the
next few sections describe the hierarchy of Java's exception classes as well as some of the
exception classes in more detail.
Java Exception Hierarchy
In the preceding sections, you learned ways to handle exceptions in a program. Chapter 8
discussed how to create your own classes. Every class you design can potentially cause
exceptions. Java provides extensive support for exception handling by providing a
number of exception classes. Java also allows users to create and implement their own
exception classes to handle exceptions not covered by Java's exception classes. This
section discusses the exception classes provided by Java.
The class Throwable , which is derived from the class Object , is the superclass of the
class Exception , as shown in Figure 11-1.
1
1
Object
Throwable
Exception
FIGURE 11-1 Java exception hierarchy
 
 
Search WWH ::




Custom Search