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();
}
}
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
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:
that was executed to cause the method to be invoked.