最后和没有最终有什么区别?
有什么区别
try {
// action A
}
catch(Exception e) {
// action B
}
finally {
// action C
}
,
try {
// action A
}
catch(Exception e) {
// action B
}
// action C
我读到您可以从 catch
块内部返回,并且仍然执行 finally
块。还有其他区别吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
无论 try-catch-块中发生什么,
finally
块中发生的事情都一定会发生。如果发生了未由Exception
封装的异常(例如,扩展Throwable
,例如各种Error
),那么它仍然 运行finally
块。需要注意的一件事是:如果在
finally
块内抛出一个RuntimeException
,或者另一个Exception
从其中逃逸,那么finally
块的其余将不会执行。此外,正如 Torgamus 勋爵指出的那样,这取决于 JVM 的运行。此外,很明显,这还取决于线程不被停止。Things that happen within the
finally
block are guaranteed to occur no matter what happens in the try-catch-block. If an exception happens that is not encapsulated byException
(e.g., extendsThrowable
, such as variousError
s), then it still runs thefinally
block.One thing to be aware of: if, within the
finally
block, aRuntimeException
is thrown, or anotherException
escapes from within it, then the rest of thefinally
block will not execute. Also, as Lord Torgamus pointed out, it is contingent on the JVM running. In addition, and probably obviously, it is also contingent on the thread not being stopped.大多数现有答案都包含正确答案的一部分,但没有一个是完全准确的。
如果 JVM 未关闭,
finally
块始终保证在try
和潜在的catch
块之后到达事先下来。但是,如果finally
块中的一些代码关闭了 JVM,或者抛出了自己的异常,则可能无法到达该块的末尾。根据Sun 认证程序员 Java 6 学习指南:
与往常一样,最终的决定是Java 语言规范。
finally
的行为在 §14.20.2 try-catch-finally 的执行。作为额外说明:您是对的,
try
中的return
不会阻止finally
运行。事实上,finally
是在遇到return
之后、执行之前立即输入的。Most of the existing answers contain pieces of the right answer, but none are quite spot-on.
The
finally
block is always guaranteed to be reached after thetry
and potentiallycatch
blocks if the JVM does not shut down beforehand. However, if a bit of code inside thefinally
block shuts down the JVM, or throws an exception its own, the end of the block might not be reached.Per the Sun Certified Programmer for Java 6 Study Guide:
The final word is, as always, the Java Language Specification. The behavior of
finally
is explained exhaustively in §14.20.2 Execution of try-catch-finally.As an extra note: you're right that a
return
in thetry
won't stopfinally
from running. In fact,finally
is entered immediately after thereturn
is encountered, before it executes.更好地看看这个例子,即使我返回或传播异常,资源也总是关闭的。
如果动作 A 和 be 和 int a = 0 一样原始,那么没有区别,但在像这样更复杂的情况下,finnaly 块有它的用法
Better look at this example, resources are allways closed even if I return or propagate the Excpetion up.
If action A and be are as primitve as int a = 0, then there is no difference, but in more complicated situations like this, finnaly block have it's usage
来自 Sun 教程
我不知道还有什么其他方法finally 块不会执行......
from the Sun Tutorials
I don't know of any other ways the finally block wouldn't execute...
如果 JVM 继续运行,
finally
块内的代码将保证执行。这意味着即使操作 B 中的内容抛出另一个新异常或未捕获操作 A 中的异常或调用了 return,操作 C 中的代码也将运行。
Code inside the
finally
block is guaranteed to execute should the JVM continue running.This means that even if what is in action B throws another new exception or doesn't catch the exception in action A or a return is called, the code in action C will run.
try 块用于异常处理。如果有任何情况/代码会抛出异常,例如,如果我们将一个数字除以零(0),那么它将抛出异常并且进程将被关闭。在这种情况下,如果我们将代码放在 try 块中,则异常会被 catch 块捕获,并且进程不会终止。最后块保证执行在那里编写的代码。因此,如果我们必须终止/关闭进程,无论其执行是否成功(例如,在网络编程的情况下,我们必须最后释放连接,以便其他设备可以使用该连接),我们使用finally块。
A try block is used for exception handling. If there any case/code that will throw exception, e.g., if we divide a number by Zero(0) than it will throw an exception and the process will be shutdown. In this case if we put our code in the try block than the exception is catch by the catch block and the process will not be terminated. And the finally block the guarantee of executing the code written there. Therefore, if we have to terminate/close the process even its execution is successful or not(e.g., in case of network programming we have to release the connection at last so that other devices can use that connection) we use the finally block.