你真的需要'最后'块 [英] Do you really need the 'finally' block

查看:96
本文介绍了你真的需要'最后'块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有3个排列的try ... catch ... finally在java中阻止。

There are 3 permutations of a try...catch...finally block in java.


  1. try ... catch

  2. try ... catch ... finally

  3. try ... finally

一旦finally块被执行,控制将进入finally块之后的下一行。如果我删除finally块,并将所有的语句移动到try ... catch块后面的行,那么它们会在finally块中具有相同的效果?

Once the finally block is executed, control goes to the next line after the finally block. If I remove the finally block and move all its statements to the line after the try...catch block, would that have the same effect as having them in the finally block?

推荐答案

我认为willcode最接近在这里表达要点,可能大家都意味着,但不清楚。

I think willcode comes the closest to expressing the key point here, and probably everyone means it but are not clear.

问题在于你所要问的确是有些错误的:如果我把所有的语句写在catch块之后,而不是把它们写入finally块,那么会有什么问题吗?

The problem is there is indeed something very wrong with what you are asking: "If i write all the statements after catch block instead of writing them into finally block then then would there be anything wrong?"

如果您在catch块之后编写所有语句,您所暗示的是

If you write all the statements after the catch block, what you are implying is that

1)您将总是捕获异常。

1) You will always catch the exception.

2)在您捕获异常之后,您将始终继续下一个语句。

2) You will always continue on to the next statements after you catch the exception.

这意味着您将永远继续执行异常后的正常,这通常是您 永远不会 / strong>其实是想做的。

This implies that you will always continue the execution "normally" after an exception, which is generally something you never in fact want to do.

异常应该是这样 - 例外。如果你实际上可以处理一个异常,最好编写你的代码来考虑这些条件,而不会导致异常。如果你遵循这个模式,那么例外是真正的例外 - 你无法预料或最多没有修复的条件。真的不能期待你应该努力。这意味着一般来说,你无法处理真正的异常,这也意味着你不应该继续执行,而是通常你结束应用程序。

Exceptions should be just that - exceptional. If you can in fact handle an exception, it is always better to write your code to consider those conditions first and not lead to an exception at all. If you follow this model then exceptions are truly exceptional - conditions you could not anticipate or at most not fix. Really not anticipate is what you should work towards. This means in general you are unable to handle true exceptions, which also means you should not just continue execution, often you end the application instead.

通常做的是允许一个错误传播备份调用堆栈。有人说这是在一个很高的机会,一个更高的链可能可以处理它。我会说,基本上没有发生,有两个真正的目的来做到这一点。一个可能是用户可以修复的东西,如果有的话。所以您传播错误备份,直到您到达可以向用户报告的位置。或者两个,用户无法修复它,但是您想要获取整个调用堆栈进行调试。然后你在顶部抓住它,以优雅地失败。

What is normally done is you allow an error to propagate back up the call stack. Some say this is done on the off chance that someone higher up in the chain may be able to handle it. I would say that essentially never happens, there are two real purposes to do this. One it may be something the user can fix, if there is one. So you propagate the error back up until you get to where you can report it to the user. Or two, a user cannot fix it but you want to get the entire call stack for debugging. Then you catch it at the top to fail gracefully.

最后的块现在应该对你有更多的意义。众所周知,它总是运行。最终使用最终是真的在尝试...终于阻止。你现在说的是如果代码运行正常,很好。我们还需要做一些清理,最后总是执行,然后我们继续前进。但是,如果发生异常,我们现在真的需要最后的阻止,因为我们可能仍然需要做一些清理,但是我们不再在这里捕获异常,所以我们不会再移动了。最后的块对于确保清理发生是至关重要的。

The finally block now should have more meaning to you. As everyone says it always runs. The clearest use of a finally is really in a try... finally block. What you are now saying is if the code runs fine, great. We still need to do some clean up and the finally always executes then we move on. But if an exception occurs, we now really need that finally block because we may still need to do some clean up, but we are no longer catching the exception here so we are not going to be moving on anymore. The finally block is essential to ensure that clean up occurs.

一个异常总是停止执行的想法可能很难让人掌握,直到他们有一定的经验,但这实际上是总是做事情的方式。如果发生错误,要么是这么小,你应该考虑到它开始,否则只有越来越多的错误等待发生在线上。

The idea of an exception always halting execution may be hard for someone to grasp until they have a certain amount of experience, but that is in fact the way to always do things. If an error happened, either it was so minor you should have accounted for it to begin with, or else there are just more and more errors waiting to happen down the line.

吞咽错误 - 捕捉和移动是您可以做的最糟糕的事情,因为您的程序变得不可预测,您无法找到并修复错误。

"Swallowing" errors - catching them and moving on is the worst thing you can do because your program becomes unpredictable and you cannot find and fix bugs.

写好好的代码将包含尽可能多的try ... finally块是必要的,以确保资源总是释放,无论结果如何。但写好的代码通常只包含少量的try ... catch块,主要用于允许应用程序尽可能优雅地失败,或者延迟到用户,这意味着至少总是传递消息给用户等。但是你通常不会发现错误并继续进行。

Well written code will contain as many try ... finally blocks as are necessary to make sure that resources are always released no matter the outcome. But well written code generally contain only a small number of try ... catch blocks that exist primarily to allow an application to fail as gracefully as possible, or defer to the user, which means at least always pass a message to the user etc. But you usually do not just catch an error and keep going.

这篇关于你真的需要'最后'块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆