posted 19 years ago
SHORT ANSWER
If an exception happens in the try block, and you handle it, and manage to pass to the next statement out of the try-catch-finally block, then that statement is actually reachable.
Not the case of your first example, pal.
Regards,
Edwin Dalorzo
LONG ANSWER
I have an interpretation of this behavior based on the explanations of the JLS chapter 14 section 21.
The JLS determines if a statement is reachable based on two principles
Whether the statement is reachable. Whether the statement can complete normally.
Now, the JLS states that a break, continue, return, or throw statement cannot complete normally.
Now, this sounds kind of obvious for some statements.
Interpretation #1
That means any statement after any of this statements could be considered unreachable.
Evidence #1
Interpretation #2
Then, JSL suggests that a finally block is reachable if its try statement is reachable. So in this code the finally statement is reachable, no matter that the try block finish abnormally.
Evidence #2
However, the JLS also states that �[�] Every other statement S in a nonempty block that is not a switch block is reachable if and only if the preceding statement S can complete normally�
Interpretation #3
As the try block in previous statement did not complete normally, the next statement after the try-finally block is unreachable, as this evidence shows.
Evidence #3
Finally, the JLS states that a Catch block is reachable "[...] if some expression or a throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause [...]"
Interpretation #4
This statement seems to be override the previous one, because althought the return stament always finish abnormally it might be subject to cause an exception in a try-catch-block, and hence the next evidence is valid, because the cath block is subjet to finish normally if you handle the exception, and if so, the statement after try-catch-finally may be executed. Let's see some evidence
Evidence #4
I hope this helps.
Regards,
Edwin Dalorzo