Java Reference
In-Depth Information
The Compiler and Checked Exceptions
The compiler checks each method call and method declaration to determine whether the
method throws a checked exception. If so, the compiler verifies that the checked exception
is
caught
or is
declared
in a
throws
clause—this is known as the
catch-or-declare require-
ment
. We show how to catch or declare checked exceptions in the next several examples.
Recall from Section 11.3 that the
throws
clause specifies the exceptions a method throws.
Such exceptions are not caught in the method's body. To satisfy the
catch
part of the
catch-
or-declare requirement
, the code that generates the exception must be wrapped in a
try
block and must provide a
catch
handler for the checked-exception type (or one of its su-
perclasses). To satisfy the
declare
part of the catch-or-declare requirement, the method
containing the code that generates the exception must provide a
throws
clause containing
the checked-exception type after its parameter list and before its method body. If the
catch-or-declare requirement is not satisfied, the compiler will issue an error message. This
forces you to think about the problems that may occur when a method that throws
checked exceptions is called.
Error-Prevention Tip 11.2
You must deal with checked exceptions. This results in more robust code than would be
created if you were able to simply ignore them.
Common Programming Error 11.2
If a subclass method overrides a superclass method, it's an error for the subclass method to
list more exceptions in its
throws
clause than the superclass method does. However, a sub-
class's
throws
clause can contain a subset of a superclass's
throws
clause.
Software Engineering Observation 11.5
If your method calls other methods that throw checked exceptions, those exceptions must
be caught or declared. If an exception can be handled meaningfully in a method, the
method should catch the exception rather than declare it.
The Compiler and Unchecked Exceptions
Unlike checked exceptions, the Java compiler does
not
examine the code to determine
whether an unchecked exception is caught or declared. Unchecked exceptions typically
can be
prevented
by proper coding. For example, the unchecked
ArithmeticException
thrown by method
quotient
(lines 9-13) in Fig. 11.3 can be avoided if the method en-
sures that the denominator is not zero
before
performing the division. Unchecked excep-
tions are
not
required to be listed in a method's
throws
clause—even if they are, it's
not
required that such exceptions be caught by an application.
Software Engineering Observation 11.6
Although the compiler does not enforce the
catch
-or-declare requirement for unchecked
exceptions, provide appropriate exception-handling code when it's known that such excep-
tions might occur. For example, a program should process the
NumberFormatException
from
Integer
method
parseInt
, even though
NumberFormatException
is an indirect
subclass of
RuntimeException
(and thus an unchecked exception). This makes your pro-
grams more robust.