Java Reference
In-Depth Information
Figure 20.3. Of the classes shown, only
Throwable
,
Exception
, and
IOException
must be handled.
Exceptions are normally
checked,
meaning they must be declared by a method that might
throw them. The
Error
class and its subclasses and the
RuntimeException
class and its
subclasses are
unchecked,
meaning that they need not be declared.
The Javaâ„¢Language
Specification
(Gosling et al. 2000) says:
Error
and its subclasses are exempted from compile-time checking because
they can occur at many points in the program and recovery from them is
difficult or impossible. (p. 221)
and
RuntimeException
and its subclasses are exempted from compile-time
checking because, in the judgment of the designers of the Java programming
language, having to declare such exceptions would not aid significantly in
establishing the correctness of programs. (p. 222)
The existence of unchecked exceptions means that methods may throw exceptions that you
don't have to declare. For example, you can perform casts without handling
ClassCastException
in either a
try/catch
statement or in the signature of the method.
CHALLENGE 20.5
The designers of the Java programming language created a distinction between
checked and unchecked exceptions. Explain why you think this was a good idea or
a bad idea.