An exception is an anomalous condition that alters or interrupts the flow of execution. Java provides built-in exception handling to deal with such conditions. Exception handling should not be part of the normal program flow.
As shown in Figure 7-1, all exceptions and errors inherit from the class
Throwable, which inherits from the class
Exceptions and errors fall into three categories: checked exceptions, unchecked exceptions, and errors.
Checked exceptions are checked by the compiler at compile time.
Methods that throw a checked exception must indicate so in the method declaration using the
throws clause. This must continue all the way up the calling stack until the exception is handled.
All checked exceptions must be explicitly caught with a
Checked exceptions include exceptions of the type
Exception, and all classes that are subtypes of
Exception, except for
RuntimeException and the subtypes of
The following is an example of a method that throws a checked exception:
// Method declaration that throws
// an IOException
The compiler does not check unchecked exceptions at compile time. ...