Java 当涉及到 finally 块的返回值时,在 try 块中包含 return 语句是一种好习惯吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3643002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it good practice to have return statement in try block when returned value from finally block is concerned
提问by Cheok Yan Cheng
I was wondering, is it good practice to return
from try
block?
我想知道,return
从try
块中取出是一种好习惯吗?
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 :
我首先想到false
会被打印。但是,这是输出:
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.
如您所见,如果我return
在try
块内有语句,我将错过finally
清理期间的失败状态。
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?
只要涉及到 finally 块的返回值,我应该避免从 try 返回吗?
采纳答案by Bozho
Your suggested code (at the end of the question) is fine. You canreturn from the finally
block, but you should not - for example eclipse shows a warning "finally block does not complete normally".
您建议的代码(在问题末尾)很好。您可以从finally
块中返回,但不应该 - 例如 eclipse 显示警告“最后块没有正常完成”。
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.
事实上,try/finally
它们与return
. 在这里似乎是这样,因为它是方法中唯一的构造,但是在那之后您可以有其他代码(例如 - 事件通知),然后返回。
As for your question - you can't change the value of the returned variablein the finally
block if it is already returned. So don't return from try
.
至于你的问题-你不能改变的值返回的变量中finally
,如果已经返回它块。所以不要从try
.
回答by YoK
Answer for why "true" is returned:
为什么返回“true”的答案:
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)
如果从 try 返回变量,虽然返回的变量值在 finally 块中发生了变化,但先前设置的值(在这种情况下在 try 块中设置的值)将被返回。(当然在 finally 中没有 return 语句)
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. :
如果您希望更改要在 finally 块中返回的值,请遵循您的第二种方法。IE :
static boolean fun() {
boolean everything_is_fine = true;
try {
System.out.println("open file stream");
} finally {
everything_is_fine = cleanup();
}
return everything_is_fine;
}
回答by NullUserException
While it's considered bad practice, you canreturn on finally
. Doing so also trumps any other return
s you might've had in your try
and catch
blocks. Proof:
虽然它被认为是不好的做法,但您可以返回finally
. 这样做也胜过return
您可能在您的try
和catch
块中拥有的任何其他s 。证明:
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)
该finally
块总是被执行(禁止调用System.exit()
或拔下电源插头)
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.
编辑:请注意,在您的第一个代码示例中,finally
块中没有返回任何内容,但是如果您在return false
那里有一个,您的方法将始终返回 false。
回答by dsmith
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.
如果你需要返回一些依赖于在 finally 块中运行的代码的东西,那么你需要把你的 return 放在 try 块之外;正如其他人所说,只有一个 return 语句是一种很好的做法。
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.
话虽如此,我从未遇到过我的返回值依赖于 finally 块中的计算的情况,我通常会将返回值放在 try 中。
回答by samitgaur
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.
该return
语句指示返回什么值,在return
执行该语句时是什么值true
。finally
确实改变了变量的值everything_is_fine
,但这不会改变已经执行的return
语句返回的内容。
You couldadd another return in finally
which will override the return
inside try
:
您可以添加另一个 returnfinally
其中将覆盖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 iscertainly possible though. A better way of doing this in your case would be:
但是,使用finally
来修改控制流不被认为是好的做法。这是绝对有可能,但。在您的情况下这样做的更好方法是:
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;-)
顺便说一句,变量名应该根据everythingIsFine
现行的Java 命名约定更改为;-)
回答by user207421
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?
finally 块中对 everything_is_fine 的赋值不会影响返回的内容。对我来说,这似乎是一种糟糕的做法。意图是什么?