使用finally块与在try/catch块之后编写代码
据我了解,以下两个示例应该做同样的事情。为什么第一个被认为更好?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,一方面,如果“处理异常”部分本身引发异常,则不会发生清理。
但更重要的是,您几乎永远应该捕获所有异常。您应该捕获可以处理的非常具体的异常,并让其他异常冒泡。此时,如果您希望清理仍然发生,则必须使用
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 areturn
statement. The very fact that I'd forgotten that is a good reason to favourfinally
: 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.因为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. :)
因为“最后”清楚地表明您正在此处进行清理。
Because "finally" is a clear indication that you're doing the cleanup here.