Why returning to try does not end the method?
Is it really bad practice to return from a finally catch try block?
You should know that when you put a return inside a try-catch statement, the return itself is held and controlled by the exeption processing unit.
The exception block only records your return, and it really returns it ONLY after all the checks have been completed, which does not mean that the finally block will really affect your return:
public int returnVal() { int i = 0; try { return i; } finally { i = 99; } }
In this case, the return will be 0.
But in the case that you mentioned in your question, you have an impact on the return a second time, and since the exception block does not want your return to pass until it is completed with all its validation, you always save the return value 2 times to your exception block. The result will always be 3.
Just keep in mind that if you put the answer inside try-catch-finally, this return will not return until the last block completes; the return itself is only saved until everything is verified.
source share