C# 从 try catch finally 块中返回是不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449099/
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 bad practice to return from within a try catch finally block?
提问by lomaxx
So I came across some code this morning that looked like this:
所以我今天早上遇到了一些看起来像这样的代码:
try
{
x = SomeThingDangerous();
return x;
}
catch (Exception ex)
{
throw new DangerousException(ex);
}
finally
{
CleanUpDangerousStuff();
}
Now this code compiles fine and works as it should, but it just doesn't feel right to return from within a try block, especially if there's an associated finally.
现在这段代码编译得很好并且可以正常工作,但是从 try 块中返回感觉不正确,特别是如果有关联的 finally。
My main issue is what happens if the finally throws an exception of it's own? You've got a returned variable but also an exception to deal with... so I'm interested to know what others think about returning from within a try block?
我的主要问题是如果最终抛出它自己的异常会发生什么?您有一个返回的变量,但也有一个异常要处理……所以我很想知道其他人对从 try 块中返回的看法?
采纳答案by Mehrdad Afshari
No, it's not a bad practice. Putting return
where it makes sense improves readability and maintainability and makes your code simpler to understand. You shouldn't care as finally
block will get executed if a return
statement is encountered.
不,这不是一个坏习惯。把return
它放在有意义的地方可以提高可读性和可维护性,并使您的代码更易于理解。finally
如果return
遇到语句,您不应该关心块将被执行。
回答by Spencer Ruport
This may answer your question
这可能会回答你的问题
What really happens in a try { return x; } finally { x = null; } statement?
在 try { return x; 中真正发生了什么?} 最后{ x = null; } 陈述?
From reading that question it sounds like you can have another try catch structure in the finally statement if you think it might throw an exception. The compiler will figure out when to return the value.
通过阅读该问题,如果您认为它可能会引发异常,那么您可以在 finally 语句中使用另一个 try catch 结构。编译器将确定何时返回该值。
That said, it might be better to restructure your code anyway just so it doesn't confuse you later on or someone else who may be unaware of this as well.
也就是说,无论如何重组你的代码可能会更好,这样以后就不会混淆你或其他可能不知道这一点的人。
回答by Ed S.
The finally will be executed no matter what, so it doesn't matter.
finally 无论如何都会被执行,所以没有关系。
回答by BeWarned
In your example either way is equivalent, I wouldn't even be suprised if the compiler generated the same code. If an exception happens in the finally block you have the same issues whether you put the return statement in block or outside of it.
在您的示例中,任何一种方式都是等效的,如果编译器生成相同的代码,我什至不会感到惊讶。如果在 finally 块中发生异常,无论是将 return 语句放在块中还是块之外,都会遇到相同的问题。
The real question is stylistically which is best. I like to write my methods so that there is only one return statement, this way it is easier to see the flow out of the method, it follows that I also like to put the return statement last so it is easy to see that it is the end of the method and this what it returns.
真正的问题是在风格上哪个最好。我喜欢把我的方法写成只有一个 return 语句,这样更容易看到方法的流程,因此我也喜欢把 return 语句放在最后,所以很容易看出它是方法的结尾以及它返回的内容。
I think with the return statement so neatly placed as the last statement, others are less likely to come and sprinkle multiple returns statements into other parts of the method.
我认为 return 语句像最后一个语句一样整齐地放置,其他人不太可能来将多个 return 语句散布到方法的其他部分。
回答by Conrad
Personally, I would avoid this kind of coding as I don't feel like seeing return statements before finally statements.
就个人而言,我会避免这种编码,因为我不想在 finally 语句之前看到 return 语句。
My mind is simple and it process things rather linearly. Therefore when I walk through the code for dry running, I will have tendency to think that once I can reach the return statement, everything follow doesn't matter which obviously is pretty wrong in this case (not that it would affect the return statement but what the side effects could be).
我的头脑很简单,它处理事情的过程相当线性。因此,当我遍历干运行代码时,我会倾向于认为一旦我可以到达 return 语句,接下来的一切都无关紧要,这在这种情况下显然是错误的(不是说它会影响 return 语句,而是可能有什么副作用)。
Thus, I would arrange the code so that the return statement always appear after the finally statements.
因此,我会安排代码,以便 return 语句始终出现在 finally 语句之后。
回答by Ifeanyi Echeruo
Functionally there is no difference.
功能上没有区别。
However there is one reason for not doing this. Longer methods with several exit points are often more difficult to read and analyze. But that objection has more to do with return statements than catch and finally blocks.
然而,不这样做是有原因的。具有多个退出点的较长方法通常更难以阅读和分析。但是这种反对意见更多地与 return 语句有关,而不是 catch 和 finally 块。