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?
In general both block should declare an exit. If in try you have return you should have also it in catch, in your case the second return was replaced with throw.
Yes, we can write a return statement of the method in catch and finally block.
Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.
When try and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by try block.
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 ;-)
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