Java Reference
In-Depth Information
ClassNotFoundException
ArithmeticException
IOException
Exception
NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Throwable
Object
IllegalArgumentException
LinkageError
Many more classes
Error
VirtualMachineError
Many more classes
F
IGURE
12.1
Exceptions thrown are instances of the classes shown in this diagram, or of subclasses of one of these classes.
Note
The class names
Error
,
Exception
, and
RuntimeException
are somewhat con-
fusing. All three of these classes are exceptions, and all of the errors occur at runtime.
The
Throwable
class is the root of exception classes. All Java exception classes inherit
directly or indirectly from
Throwable
. You can create your own exception classes by extend-
ing
Exception
or a subclass of
Exception
.
The exception classes can be classified into three major types: system errors, exceptions,
and runtime exceptions.
System errors
are thrown by the JVM and are represented in the
Error
class. The
Error
class describes internal system errors, though such errors rarely occur. If one
does, there is little you can do beyond notifying the user and trying to terminate the
program gracefully. Examples of subclasses of
Error
are listed in Table 12.1.
system error
■
T
ABLE
12.1
Examples of Subclasses of
Error
Class
Reasons for Exception
LinkageError
A class has some dependency on another class, but the latter class has
changed incompatibly after the compilation of the former class.
VirtualMachineError
The JVM is broken or has run out of the resources it needs in order to
continue operating.
Exceptions
are represented in the
Exception
class, which describes errors caused by
your program and by external circumstances. These errors can be caught and handled
by your program. Examples of subclasses of
Exception
are listed in Table 12.2.
exception
■
T
ABLE
12.2
Examples of Subclasses of
Exception
Class
Reasons for Exception
ClassNotFoundException
Attempt to use a class that does not exist. This exception would occur, for example, if you tried to
run a nonexistent class using the
java
command, or if your program were composed of, say,
three class files, only two of which could be found.
IOException
Related to input/output operations, such as invalid input, reading past the end of a file, and opening
a nonexistent file. Examples of subclasses of
IOException
are
InterruptedIOException
,
EOFException
(EOF is short for End of File), and
FileNotFoundException
.
Search WWH ::
Custom Search