Is it good practice to have return statement in try block when returned value from finally block is concerned
I was wondering, is it good practice to return
from try
block?
package debug;
/**
*
* @author Own开发者_StackOverflow社区er
*/
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 the return
. 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 from try
.
The return
statement dictates what value is being returned, which at the time the return
statement is executed is true
. finally
does change the value of the variable everything_is_fine
, but that doesn't change what the already executed return
statement returned.
You could add another return in finally
which will override the return
inside try
:
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
return everything_is_fine;
} finally {
everything_is_fine = cleanup();
return everything_is_fine;
}
}
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:
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
} finally {
everything_is_fine = cleanup();
}
return everything_is_fine;
}
Btw, the variable name should be changed to everythingIsFine
per the prevailing Java naming conventions ;-)
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. :
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
} finally {
everything_is_fine = cleanup();
}
return everything_is_fine;
}
While it's considered bad practice, you can return on finally
. Doing so also trumps any other return
s you might've had in your try
and catch
blocks. Proof:
class Main
{
public static String test() {
try {
except();
return "return from try";
} catch (Exception e) {
return "return from catch";
} finally {
return "return from finally";
}
}
public static void except() throws Exception {
throw new Exception();
}
public static void main(String[] args) {
System.out.println(test());
}
}
Will print "return from finally". See it on ideone.
The finally
block is always executed (barred a call to System.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 a return false
there, your method would always return false.
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?
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.
精彩评论