从try catch finally块中返回是不好的做法吗?

时间:2022-06-02 22:19:57

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块中返回它感觉不对,特别是如果最后有关联的话。

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块中返回的想法?

6 个解决方案

#1


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块将被执行。

#2


The finally will be executed no matter what, so it doesn't matter.

最终将无论如何执行,所以没关系。

#3


Personally, I would avoid this kind of coding as I don't feel like seeing return statements before finally statements.

就个人而言,我会避免这种编码,因为我不想在最终陈述之前看到返回语句。

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).

我的思维很简单,它可以线性地处理事物。因此,当我通过代码进行干运行时,我会倾向于认为一旦我能够达到返回语句,一切跟随无关紧要在这种情况下显然是错误的(不是它会影响返回语句但是副作用可能是什么)。

Thus, I would arrange the code so that the return statement always appear after the finally statements.

因此,我会安排代码,以便return语句始终出现在finally语句之后。

#4


This may answer your question

这可以回答你的问题

What really happens in a try { return x; } finally { x = null; } statement?

尝试中真正发生的事情{return x; } finally {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.

也就是说,不管怎么说重组你的代码可能会更好,这样以后就不会让你感到困惑,或者其他人也可能没有意识到这一点。

#5


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块。

#6


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.

我认为返回语句如此巧妙地放在最后一个语句中,其他人不太可能将多个返回语句洒到方法的其他部分。

#1


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块将被执行。

#2


The finally will be executed no matter what, so it doesn't matter.

最终将无论如何执行,所以没关系。

#3


Personally, I would avoid this kind of coding as I don't feel like seeing return statements before finally statements.

就个人而言,我会避免这种编码,因为我不想在最终陈述之前看到返回语句。

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).

我的思维很简单,它可以线性地处理事物。因此,当我通过代码进行干运行时,我会倾向于认为一旦我能够达到返回语句,一切跟随无关紧要在这种情况下显然是错误的(不是它会影响返回语句但是副作用可能是什么)。

Thus, I would arrange the code so that the return statement always appear after the finally statements.

因此,我会安排代码,以便return语句始终出现在finally语句之后。

#4


This may answer your question

这可以回答你的问题

What really happens in a try { return x; } finally { x = null; } statement?

尝试中真正发生的事情{return x; } finally {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.

也就是说,不管怎么说重组你的代码可能会更好,这样以后就不会让你感到困惑,或者其他人也可能没有意识到这一点。

#5


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块。

#6


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.

我认为返回语句如此巧妙地放在最后一个语句中,其他人不太可能将多个返回语句洒到方法的其他部分。