The Java language checks, at compile time, that a Java program contains handlers
for checked exceptions, by analyzing which checked exceptions can result from
execution of a method or constructor. For each checked exception which is a possible result, the throws
clause for the method (§8.4.4) or constructor (§8.6.4)
must mention the class of that exception or one of the superclasses of the class of
that exception. This compile-time checking for the presence of exception handlers
is designed to reduce the number of exceptions which are not properly handled.
The unchecked exceptions classes are the class RuntimeException
and its subclasses, and the class Error
and its subclasses. All other exception classes are checked exception classes. The standard Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by Java programmers. See §11.5 for a description of the Java exception class hierarchy and the exception classes defined by the standard Java API and Java Virtual Machine.
The checked exception classes named in the throws
clause are part of the contract between the implementor and user of the method or constructor. The throws
clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws
clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws
clause that is compatible with all the overridden declarations (§9.4).
Variable initializers for fields (§8.3.2) and static initializers (§8.5) must not result in a checked exception; if one does, a compile-time error occurs.