Java Reference
In-Depth Information
nested inside a
try
-
catch
or
try
-
finally
or
try
-
catch
-
finally
statement:
try {
try
ResourceSpecification
Block
}
Catches
opt
Finally
opt
The effect of the translation is to put the
ResourceSpecification
“inside” the
try
statement.
This allows a
catch
clause of an extended
try
-with-resources statement to catch an exception
due to the automatic initialization or closing of any resource.
Furthermore, all resources will have been closed (or attempted to be closed) by the time the
finally
block is executed, in keeping with the intent of the
finally
keyword.
14.21. Unreachable Statements
It is a compile-time error if a statement cannot be executed because it is
unreachable
.
This section is devoted to a precise explanation of the word “reachable.” The idea is
that there must be some possible execution path from the beginning of the construct-
or, method, instance initializer, or static initializer that contains the statement to the
statement itself. The analysis takes into account the structure of statements. Except for
the special treatment of
while
,
do
, and
for
statements whose condition expression has
the constant value
true
, the values of expressions are not taken into account in the flow
analysis.
For example, a Java compiler will accept the code:
{
int n = 5;
while (n > 7) k = 2;
}
even though the value of
n
is known at compile time and in principle it can be known
at compile time that the assignment to
k
can never be executed.
The rules in this section define two technical terms:
• whether a statement is
reachable
• whether a statement
can complete normally
The definitions here allow a statement to complete normally only if it is reachable.