Java Reference
In-Depth Information
try {
m();
// Method m's declaration says "throws IOException".
// m "can throw" IOException. It is not the case
// that m "can throw" a subtype or supertype of
// IOException, e.g. Exception, though Exception or
// a supertype of Exception can always be caught.
} catch (FileNotFoundException fnfe) {
// Legal in Java SE 6 and 7, because the dynamic type
// of the IOException might be FileNotFoundException.
} catch (IOException ioe) {
// Legal in Java SE 6 and 7.
} catch (Throwable t) {
// Legal in Java SE 6 and 7.
}
}
static void m() throws IOException {
throw new FileNotFoundException();
}
}
By the rules above, each alternative in a multi- catch clause (§ 14.20 ) must be able to
catch some exception class thrown by the try block and uncaught by previous catch
clauses. For example, the second catch clause below would cause a compile-time error
because exception analysis determines that SubclassOfFoo is already caught by the first
catch clause:
try { ... }
catch (Foo f) { ... }
catch (Bar | SubclassOfFoo e) { ... }
11.3. Run-Time Handling of an Exception
When an exception is thrown (§ 14.18 ), control is transferred from the code that caused
the exception to the nearest dynamically enclosing catch clause, if any, of a try statement
14.20 ) that can handle the exception.
A statement or expression is dynamically enclosed by a catch clause if it appears within the
try block of the try statement of which the catch clause is a part, or if the caller of the state-
ment or expression is dynamically enclosed by the catch clause.
The caller of a statement or expression depends on where it occurs:
• If within a method, then the caller is the method invocation expression (§ 15.12 )
that was executed to cause the method to be invoked.
Search WWH ::




Custom Search