Java Reference
In-Depth Information
• The
try
block can throw
E
, or an expression used to initialize a resource (in a
try
-
with-resources statement) can throw
E
, or the automatic invocation of the
close()
method of a resource (in a
try
-with-resources statement) can throw
E
, and
E
is not
assignment compatible with any catchable exception class of any
catch
clause of
the
try
statement, and either no
finally
block is present or the
finally
block can com-
plete normally; or
• Some
catch
block of the
try
statement can throw
E
and either no
finally
block is
present or the
finally
block can complete normally; or
• A
finally
block is present and can throw
E
.
either:
• Some expression of the constructor invocation's parameter list can throw
E
; or
•
E
is determined to be an exception class of the
throws
clause of the constructor that
is invoked (§
15.12.2.6
).
Any other statement
S
can throw an exception class
E
iff an expression or statement imme-
diately contained in
S
can throw
E
.
11.2.3. Exception Checking
It is a compile-time error if a method or constructor body
can throw
some exception class
E
when
E
is a checked exception class and
E
is not a subclass of some class declared in the
throws
clause of the method or constructor.
It is a compile-time error if a class variable initializer (§
8.3.2
) or static initializer (§
8.7
)
of
a named class or interface
can throw
a checked exception class.
It is a compile-time error if an instance variable initializer or instance initializer of a named
class
can throw
a checked exception class unless that exception class or one of its super-
classes is explicitly declared in the
throws
clause of each constructor of its class and the
class has at least one explicitly declared constructor.
Note that no compile-time error is due if an instance variable initializer or instance
initializer of an anonymous class (§
15.9.5
) can throw an exception class. In a named
class, it is the responsibility of the programmer to propagate information about which
exception classes can be thrown by initializers, by declaring a suitable
throws
clause on
any explicit constructor declaration. This relationship between the checked exception
classes thrown by a class's initializers and the checked exception classes declared by a
class's constructors is assured implicitly for an anonymous class declaration, because