Java Reference
In-Depth Information
You cannot put a superclass
catch
clause before a
catch
of one of its sub-
classes. The
catch
clauses are examined in order, so a
catch
that picked
up one exception type before a
catch
for an extended type of exception
would be a mistake. The first clause would always catch the exception,
and the second clause would never be reached. The compiler will not
accept the following code:
class SuperException extends Exception { }
class SubException extends SuperException { }
class BadCatch {
public void goodTry() {
/* This is an INVALID catch ordering */
try {
throw new SubException();
} catch (SuperException superRef) {
// Catches both SuperException and SubException
} catch (SubException subRef) {
// This would never be reached
}
}
}
Only one exception is handled by any single encounter with a
TRy
clause.
If a
catch
or
finally
clause throws another exception, the
catch
clauses
of the
try
are not reexamined. The
catch
and
finally
clauses are outside
the protection of the
try
clause itself. Such exceptions can, of course,
be handled by any encompassing
try
block in which the inner
catch
or
finally
clauses were nested.
12.4.1.
finally
The
finally
clause of a
try
statement provides a mechanism for execut-
ing a section of code whether or not an exception is thrown. Usually, the
finally
clause is used to clean up internal state or to release non-object