It is sometimes said that a method should have only one return statement (i.e. one exit point) and that to code using more than one return per method is bad practice. It is claimed to be a risk to readability or a source of error. Sometimes this is even given the title of the “single exit point law”.
A function can have more than one return statement, but only ever run one based on a condition.
You can have multiple return statements and it should not matter, if you are returning the same variable. However if you have multiple variables in your function and you return then differently depending on the code flow the compiler cannot perform such optimisation.
More than one return statement may appear in a function, but only one will ever be executed by any given function call. ( All returns other than the last need to be controlled by logic such as "if" blocks. )
It does not give a compilation error because it is allowed by the Java Language Specification. However, it gives a warning message because including a return
statement in the finally
block is usually a bad idea.
What happens in your example is the following. The return
statement in the try
block is executed. However, the finally
block must always be executed so it is executed after the catch
block finishes. The return
statement occurring there overwrites the result of the previous return
statement, and so the method returns the second result.
Similarly a finally
block usually should not throw an exception. That's why the warning says that the finally
block should complete normally, that is, without return
or throwing an exception.
This is described in the Java Language Specification:
§14.17
Abrupt completion of a
finally
clause can disrupt the transfer of control initiated by areturn
statement.
§14.20.2
If execution of the
try
block completes normally, then thefinally
block is executed, and then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes normally.- If the
finally
block completes abruptly for reason S, then thetry
statement completes abruptly for reason S.If execution of the
try
block completes abruptly for any other reason R, then thefinally
block is executed, and then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes abruptly for reason R.- If the
finally
block completes abruptly for reason S, then thetry
statement completes abruptly for reason S (and reason R is discarded).
There are no compile time error since only 1 and exactly 1 of return
statement will actually return the control back to calling code.
As explained @Hoopje, return
within try
or catch
will execute first, their respective return statement will also execute. But just before returning the control back to calling code, it will execute the finally
block. Now, this block
also return
s something, so this return overrides the previous one.
It's essentially the same as this:
public boolean someMethod(){
if(1 == 1){
return true;
}
return false;
}
It will not give a compilation error, although it will give a warning. The compiler will only give an error when there's a chance of no return statement being executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With