I was wondering, is it good practice to return
from try
block?
package debug;
/**
*
* @author Owner
*/
public class Main {
public static void main(String[] args) {
System.out.println(fun());
}
static boolean cleanup() {
// Fail to cleanup.
return false;
}
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
return everything_is_fine;
} finally {
everything_is_fine = cleanup();
}
}
}
I first thought false
will be printed. However, here is the output :
open file stream
true
As you can see, if I am having return
statement within try
block, I will miss the fail status during finally
cleanup.
Shall I have the code as :
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
} finally {
everything_is_fine = cleanup();
}
return everything_is_fine;
}
As long as the returned value from finally block is concerned, shall I avoid return from try?
Your suggested code (at the end of the question) is fine. You can return from the
finally
block, but you should not - for example eclipse shows a warning "finally block does not complete normally".In fact, the
try/finally
aren't directly related to thereturn
. It seems so here, because it is the only construct in the method, but you can have other code after that (for example - event notifications), and then return.As for your question - you can't change the value of the returned variable in the
finally
block if it is already returned. So don't return fromtry
.The
return
statement dictates what value is being returned, which at the time thereturn
statement is executed istrue
.finally
does change the value of the variableeverything_is_fine
, but that doesn't change what the already executedreturn
statement returned.You could add another return in
finally
which will override thereturn
insidetry
:However, the use of
finally
to modify the control flow is not considered good practice. It is certainly possible though. A better way of doing this in your case would be:Btw, the variable name should be changed to
everythingIsFine
per the prevailing Java naming conventions ;-)While it's considered bad practice, you can return on
finally
. Doing so also trumps any otherreturn
s you might've had in yourtry
andcatch
blocks. Proof:Will print "return from finally". See it on ideone.
The
finally
block is always executed (barred a call toSystem.exit()
or pulling the power plug)EDIT: Note that in your first code sample, nothing is returned in the
finally
block but if you had areturn false
there, your method would always return false.Answer for why "true" is returned:
If variable is returned from try, though returned variables value is changed in finally block, previously set value (in this case value set in try block) will be returned. (off course there is no return statement in finally)
Answer for what you wish to achieve:
If you wish to change value to be returned in finally block then follow your second approach. i.e. :
If you need to return something that has dependencies on code run in a finally block, then you need to put your return outside the try block; as someone else stated, it's good practice to have just one return statement.
Having said this, I have never come across as case where my return value was dependant on a calculation in the finally block, and I would normally put the return inside the try.
The assignment to everything_is_fine in the finally block doesn't affect what is returned. It looks like a poor practice to me. What's the intention?