Java Reference
In-Depth Information
An implicitly final exception parameter is
final
by virtue of its declaration, while an
effectively final exception parameter is (as it were)
final
by virtue of how it is used.
An exception parameter of a multi-
catch
clause is implicitly final, so will never occur
as the left-hand operand of an assignment operator, but it is
not
considered effectively
final.
If an exception parameter is effectively final (in a uni-
catch
clause) or implicitly final
(in a multi-
catch
clause), then adding an explicit
final
modifier to its declaration will not
introduce any compile-time errors. However, if the exception parameter
of a uni-
catch
clause
is explicitly declared
final
, then removing the
final
modifier may introduce
compile-time errors. This is because the exception parameter, while still effectively
final, can no longer be referenced by, for example, local classes. On the other hand, if
there are no compile-time errors, it is possible to further change the program so that
the exception parameter is re-assigned and no longer effectively final.
The relationship of the exceptions thrown by the
try
block of a
try
statement and caught by
Exception handlers are considered in left-to-right order: the earliest possible
catch
clause
accepts the exception, receiving as its argument the thrown exception object, as specified
A multi-
catch
clause can be thought of as a sequence of uni-
catch
clauses. That is, a
catch
clause whose exception parameter type is denoted as a union
D
1
|
D
2
|
...
|
D
n
is equi-
valent to a sequence of
n
catch
clauses whose exception parameters have class types
D
1
,
D
2
, ...,
D
n
, respectively. For example, the following code:
try {
... throws ReflectiveOperationException ...
}
catch (ClassNotFoundException | IllegalAccessException ex) {
... body ...
}
is semantically equivalent to the following code:
try {
... throws ReflectiveOperationException ...
} catch (final ClassNotFoundException ex1) {