从Java中的finally块返回 [英] Returning from a finally block in Java
问题描述
似乎很多人认为这是一件坏事按照不要在finally子句中返回'。稍微深入一些,我还发现 Java的回报并不总是这显示了一些非常可怕的其他类型的流控制在最后的块中的例子。
所以,我的问题是,任何人都可以给我一个例子,其中return语句(或其他流程控制)在最后的块中产生更好/更可读的代码?
您提供的示例是足够的理由,不要从最后使用流控制。
即使有一个比较好的例子,考虑开发人员必须稍后维护你的代码,谁可能不了解微妙之处。那个糟糕的开发人员甚至可能是你....
I was surprised recently to find that it's possible to have a return statement in a finally block in Java.
It seems like lots of people think it's a bad thing to do as described in 'Don't return in a finally clause'. Scratching a little deeper, I also found 'Java's return doesn't always' which shows some pretty horrible examples of other types of flow control in finally blocks.
So, my question is, can anyone give me an example where a return statement (or other flow control) in a finally block produces better / more readable code?
The examples you provided are reason enough to not use flow-control from finally.
Even if there's a contrived example where it's "better," consider the developer who has to maintain your code later and who might not be aware of the subtleties. That poor developer might even be you....
这篇关于从Java中的finally块返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!