Java Reference
In-Depth Information
E
XCEPTION
C
LASS
H
IERARCHY
The basic class Java provides to assist in error processing is the
Exception
class.
The Java
Exception
class inherits from the
Throwable
class. The
Throwable
class is
also the superclass for the
Error
class used by the Java runtime processor to report
serious runtime errors (you should not normally have occasion to use the
Error
class directly).
The
Exception
class comes with many prebuilt subclasses. Two subclasses of
particular interest are
RuntimeException
and
IOException
. As a general rule,
Run-
timeExceptions
are created as a result of logic errors in your program. Most often,
effort should be spent in correcting and preventing this type of error instead of han-
dling it. Most Java applications attempt only to manage the
IOException
class of er-
rors, or some subclass (that is, specialization) of this error type. Figure 9.1 shows
the class hierarchy for the standard
Exception
classes.
FIGURE 9.1
Exception class hierarchy.
C
REATING
E
XCEPTIONS
A Java class can use any of the predefined
Exception
objects. These are listed in the
SDK documentation set on the CD-ROM and are also available at
www.java.sun.com.
If the standard exception definitions do not meet your requirements, you can cre-
ate new ones. Simply extend either the
Exception
or (preferably) the
IOException
class.
Sun follows certain conventions for naming
Exception
classes, and you should
follow this convention as well. In particular, classes that inherit from
Exception
or