Tim Cooke wrote:What results did you get? What results were you expecting, and why?
I expected the returns to be unreachable code except the return in finally. My results are 1 and 4 are unreachable. I don't know why 2 is reachable! I've been led to believe 1,2,and 3 should be reachable.
P.S. -- okay the return in try is short circuited by the throw so it's unreachable. I still don't have confidence in what is going on in the snippet though.
Guillermo Ishi wrote:I've been led to believe 1,2,and 3 should be reachable.
1 can never be reachable, the Exception is always thrown (and thus control flow jumps to the catch block)!
Because 2 and 3 are reachable, 4 can never be reachable. If you would comment the finally block, 4 is still unreachable because of 2. So 4 can only be reachable if you comment both return statements 2 and 3.
Now let's have some fun. If you add an always-true if-statement before the throw-statement, 1 will be reachable (but 4 still is not).
Hope it helps!
 fixed typo (thanks Mushfiq Mammadov for reporting)
Guillermo Ishi wrote:I still don't have confidence in what is going on in the snippet though.
The Exception is thrown, so the code moves to the catch block (2 is returned). But as you know, finally runs (almost) always, so the return value will be 3.
For every branch in a method (with a return ype) you need to have exactly 1 return value. If you have a finally block in your method, this block will run (almost) always. So if the finally block has a return statement (which isn't encouraged, it should always complete normally), this value will always be returned (and that's why 4 is unreachable). If you would comment the return statement in finally, the code still moves to the catch block and 2 is returned (and 4 is still unreachable).
If you would add another catch-block without a return statement (and your code doesn't have a finally block with a return statement), 4 will be reachable. Because that will be the return value for the branch of the catch-block without any return value (even if this catch block is never reached by the code inside the try-block). Illustrated in this example:
So the rules seem to be if you have a return in a finally block, anything after the finally block is always unreachable. I'm assuming here if some condition isn't met so the try that goes with the finally is never executed, then that is false..
If you do not have a finally block, then 4 is reachable (but not necessarily reached if catch has a return).
If you have a catch and a finally both with returns and an exception was thrown both returns get evaluated but the returned value is the one in finally.
As long as it's logical... None of this can't have a long in a switch block stuff...
Guillermo Ishi wrote:It seems to depend on whether the compiler knows it's unreachable without it having to think very hard...
The compiler never executes any code! That's why in your 1st snippet it doesn't know that statement will throw a runtime exception.
In the 2nd snippet the compiler knows/sees (without running any code) the exception will always be thrown. So control flow moves to catch block which has a return statement which will always be executed. So 4 will never be reached!