Java Reference
In-Depth Information
When both the
TRy
block and the
finally
block complete abruptly, the reason for the abrupt
completion in the
try
block is discarded, and the whole
TRy-finally
statement completes abruptly
for the same reason as the
finally
block. In this program, the abrupt completion caused by the
return
statement in the
TRy
block is discarded, and the
TRy
-
finally
statement completes abruptly
because of the
return
statement in the
finally
block. Simply put, the program
tries
to
return
true
but
finally
it
returns false
.
Discarding the reason for abrupt completion is almost never what you want, because the original
reason for abrupt completion might be important to the behavior of a program. It is especially
difficult to understand the behavior of a program that executes a
break
,
continue
, or
return
statement in a
TRy
block only to have the statement's behavior vetoed by a
finally
block.
In summary, every
finally
block should complete normally, barring an unchecked exception.
Never exit a
finally
block with a
return, break, continue
, or
tHRow
, and never allow a
checked exception to propagate out of a
finally
block.
For language designers,
finally
blocks should perhaps be required to complete normally in the
absence of unchecked exceptions. Toward this end, a
TRy-finally
construct would require that the
finally
block
can complete normally
[JLS 14.21]. A
return
,
break
, or
continue
statement that
transfers control out of a
finally
block would be disallowed, as would any statement that could
cause a checked exception to propagate out of the
finally
block.
< Day Day Up >
Search WWH ::
Custom Search