Java Reference
In-Depth Information
Checked exceptions
An exception is referred to as a
checked exception
if its data type is a
child class of
java.lang.Exception
, but not a child class of
RuntimeException
.
Errors
An exception is referred to as an
error
if its data type is a child class of
java
.lang.Error
. An error is associated with problems that arise outside of your application,
and you typically do not attempt to recover from errors.
Figure 3.11 shows the class hierarchy of the three types of exceptions along with
some examples of errors, checked exceptions, and runtime exceptions. You can always
determine what category an exception fi ts into by whether it subclasses
RuntimeException
,
Exception
, or
Error
.
FIGURE 3.11
The three categories of exceptions.
java.lang.Throwable
java.lang.Exception
java.lang.Error
java.lang.ClassNotFoundException
java.lang.AssertionError
java.io.IOException
java.lang.LinkageError
java.sql.SQLException
java.lang.VirtualMachineError
java.lang.InterruptedException
java.io.IOError
java.lang.RuntimeException
java.lang.ArithmeticException
java.lang.ClassCastException
java.lang.NullPointerException
You might wonder why there is such a distinct categorizing of exceptions. The categories
are important because the compiler enforces a rule known as the
Handle or Declare Rule
that only applies to checked exceptions. The Handle or Declare Rule states that if any
statement might throw a checked exception, it must do one of the following:
Handle the exception by enclosing the statement in a
try
block that provides a
corresponding handler for the exception.
The method that contains the statement must declare the checked exception in the
throws
clause of the method declaration.
Search WWH ::
Custom Search