Java Reference
In-Depth Information
Exception
is also a checked exception type in Java as is
IOException
. If a
catch
block should not catch a checked
exception unless it is thrown in the corresponding
try
block, how does the code for
CatchNonExistentException2
compile fine? Should it not generate the same compiler error? At first thought, you are right. It should fail compilation
for the same reason the
CatchNonExistentException
class failed. There are two checked exceptions classes that are
exceptions to this rule. Those two exception classes are
Exception
and
Throwable
. The
Exception
class is the superclass
of
IOException
and other exceptions, which are checked exceptions. It is also the superclass of
RuntimeException
and all subclasses of
RuntimeException
, which are unchecked exceptions. Recall the rule that a superclass exception
type can also handle a subclass exception type. Therefore, you can use the
Exception
class to handle checked
exceptions as well as unchecked exceptions. The rule of checking for
catch
blocks for un-thrown exceptions applies
only to checked exceptions.
Exception
and
Throwable
classes in a
catch
block can handle checked as well as
unchecked exceptions because they are superclasses of both types. This is the reason that the compiler will let you
use these two checked exception types in a
catch
block, even though the associated
try
block does not throw any
checked exceptions.
■
all rules about the compiler check for exceptions being handled or thrown are applicable only to checked
exceptions. Java does not force you to handle the unchecked exceptions in your code. however, you are free to handle
them as you feel appropriate to do so.
Tip
Checked Exceptions and Initializers
You cannot throw a checked exception from a
static
initializer. If a piece of code in a
static
initializer throws a
checked exception, it must be handled using a
try-catch
block inside the initializer itself. The
static
initializer is
called only once for a class, and the programmer does not have a specific point in code to catch it. This is the reason
that a
static
initializer must handle all possible checked exceptions that it may throw.
public class Test {
static {
// Must use try-catch blocks to handle all checked exceptions
}
}
The rule is different for instance initializers. An instance initializer is called as part of a constructor call for the
class. It may throw checked exceptions. However, all those checked exceptions must be included in the
throws
clause
of all constructors for that class. This way, the compiler can make sure all checked exceptions are taken care of by
programmers when any of the constructors are called. The following code for the
Test
class assumes that the instance
initializer throws a checked exception of a
CException
type. The compiler will force you to add a
throws
clause with
CException
to all constructors of
Test
.
public class Test {
// Instance initializer
{
// Throws a checked exception of type CException
}
// All constructors must specify that they throw CException
// because the instance initializer throws CException
public Test() throws CException {
// Code goes here
}