Java Reference
In-Depth Information
no explicit constructor declarations are possible and a Java compiler always generates
a constructor with a suitable throws clause for that anonymous class declaration based
on the checked exception classes that its initializers can throw.
It is a compile-time error if a catch clause can catch checked exception class E 1 and it is not
the case that the try block corresponding to the catch clause can throw a checked exception
class that is a subclass or superclass of E 1 , unless E 1 is Exception or a superclass of Exception .
It is a compile-time error if a catch clause can catch 11.2 ) checked exception class E 1 and
a preceding catch clause of the immediately enclosing try statement can catch E 1 or a super-
class of E 1 .
A Java compiler is encouraged to issue a warning if a catch clause can catch 11.2 )
checked exception class E 1 and the try block corresponding to the catch clause can
throw checked exception class E 2 , a subclass of E 1 , and a preceding catch clause of the
immediately enclosing try statement can catch checked exception class E 3 where E 2 <:
E 3 <: E 1 .
Example 11.2.3-1. Catching Checked Exceptions
Click here to view code image
import java.io.*;
class StaticallyThrownExceptionsIncludeSubtypes {
public static void main(String[] args) {
try {
throw new FileNotFoundException();
} catch (IOException ioe) {
// Legal in Java SE 6 and 7. "catch IOException"
// catches IOException and any subtype.
}
try {
throw new FileNotFoundException();
// Statement "can throw" FileNotFoundException.
// It is not the case that statement "can throw"
// a subtype or supertype of FileNotFoundException.
} catch (FileNotFoundException fnfe) {
// Legal in Java SE 6 and 7.
} catch (IOException ioe) {
// Legal in Java SE 6 and 7, but compilers are
// encouraged to throw warnings as of Java SE 7.
// All subtypes of IOException that the try block
// can throw have already been caught.
}
Search WWH ::




Custom Search