使用finally块与在try/catch块之后编写代码

发布于 2024-12-04 16:26:18 字数 338 浏览 1 评论 0原文

据我了解,以下两个示例应该做同样的事情。为什么第一个被认为更好?

1:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
finally {
  cleanUp();
}

2:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
cleanUp();

编辑:该示例是用Java编写的,但我想知道任何语言中使用的finally块的概念

As I understand, the following 2 examples should do the same thing. Why is the 1st considered better?

1:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
finally {
  cleanUp();
}

2:

try {
  riskyMethod();
}
catch(Exception e) {
  //handle exception
}
cleanUp();

EDIT: The example is in Java, but I'm wondering about the concept of a finally block in general, as used in any language

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尤怨 2024-12-11 16:26:18

好吧,一方面,如果“处理异常”部分本身引发异常,则不会发生清理。

但更重要的是,您几乎永远应该捕获所有异常。您应该捕获可以处理的非常具体的异常,并让其他异常冒泡。此时,如果您希望清理仍然发生,则必须使用finally 块。

目前尚不清楚您使用的是什么语言,但如果它是 Java,那么已经存在差异,因为非 Exception 异常(Throwable 的其他子类)最终会在您的第一个版本中清理,但不会在您的第二个版本中清理 - 但您应该'通常甚至不会捕获Exception

就我个人而言,我发现我编写的 try/finally 块比 try/catch 或 try/catch/finally 块更多。我发现我能够真正处理异常的情况非常罕见......尽管有时我捕获一个异常只是为了将其转换为更适合我正在处理的抽象级别的异常,然后重新抛出。

编辑:正如 dj aqeel 的答案中所述,如果块完成没有异常,例如通过 return 语句,也会执行 finally 语句。事实上,我忘记了这一点,这是支持finally的一个很好的理由:它促进一致性。无论块如何退出,它都是执行清理的一个一致的位置。

另请注意,在 C# 中,您通常会对一次性资源使用 using 语句。 Java 7 有 try-with-resources陈述。

Well, for one thing if the "handle exception" part throws an exception itself, cleanup won't occur.

More importantly though, you should almost never be catching all exceptions. You should catch very specific exceptions you can handle, and let other exceptions bubble up. At that point, you must use a finally block if you want the cleanup to still occur.

It's not clear what language you're using, but if it's Java then there's already a difference, as non-Exception exceptions (other subclasses of Throwable) will end up cleaning up in your first version but not in your second - but you shouldn't even catch Exception generally.

Personally I find I write more try/finally blocks than try/catch or try/catch/finally blocks. I find it pretty rare that I can really deal with an exception... although sometimes I catch one exception just to convert it to one more appropriate for the abstraction level I'm working on, and then rethrow.

EDIT: As noted in dj aqeel's answer, finally statements are also executed if the block completes without an exception, e.g. through a return statement. The very fact that I'd forgotten that is a good reason to favour finally: it promotes consistency. It's one consistent place to perform clean-up, regardless of how the block is exited.

Also note that in C#, you'd idiomatically use a using statement for disposable resources. And Java 7 has the try-with-resources statement.

兔姬 2024-12-11 16:26:18

因为finally块中的代码始终会执行,即使您有return语句、异常或其他控制将语句留在try或catch块中……除了在finally块之前关闭机器电源之外,finally在任何情况下都将始终执行。 :)

Because the code in finally block always executes, even if you have return statements, exceptions, or other control leaving statements in try or catch blocks... finally will always execute in any case other than you power off the machine just before finally block. :)

白鸥掠海 2024-12-11 16:26:18

因为“最后”清楚地表明您正在此处进行清理。

Because "finally" is a clear indication that you're doing the cleanup here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文