最后和没有最终有什么区别?

发布于 2024-11-03 23:31:02 字数 325 浏览 4 评论 0 原文

有什么区别

try {
     // action A
}
catch(Exception e) {
     // action B
}
finally {
     // action C
}

try {
     // action A
}
catch(Exception e) {
     // action B
}
// action C

我读到您可以从 catch 块内部返回,并且仍然执行 finally 块。还有其他区别吗?

What is the difference between

try {
     // action A
}
catch(Exception e) {
     // action B
}
finally {
     // action C
}

and

try {
     // action A
}
catch(Exception e) {
     // action B
}
// action C

I have read that you can return from inside a catch block and still have the finally block execute. Are there any other differences?

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

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

发布评论

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

评论(6

梦里泪两行 2024-11-10 23:31:02

无论 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 by Exception (e.g., extends Throwable, such as various Errors), then it still runs the finally block.

One thing to be aware of: if, within the finally block, a RuntimeException is thrown, or another Exception escapes from within it, then the rest of the finally 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.

老街孤人 2024-11-10 23:31:02

大多数现有答案都包含正确答案的一部分,但没有一个是完全准确的。

如果 JVM 未关闭,finally 块始终保证在 try 和潜在的 catch 块之后到达事先下来。但是,如果finally 块中的一些代码关闭了 JVM,或者抛出了自己的异常,则可能无法到达该块的末尾。

根据Sun 认证程序员 Java 6 学习指南

  • finally-will-always-be-called 规则的唯一例外是,如果 JVM 关闭,finally 将不会被调用。< /p>

  • 仅仅因为 finally 被调用并不意味着它会完成。

与往常一样,最终的决定是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 the try and potentially catch blocks if the JVM does not shut down beforehand. However, if a bit of code inside the finally 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 only exception to the finally-will-always-be-called rule is that a finally will not be invoked if the JVM shuts down.

  • Just because finally is invoked does not mean it will complete.

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 the try won't stop finally from running. In fact, finally is entered immediately after the return is encountered, before it executes.

走过海棠暮 2024-11-10 23:31:02

更好地看看这个例子,即使我返回或传播异常,资源也总是关闭的。

try {
 //action A
 return 0;
} catch (Exception e){
 //action C
 throw new Excpetion("Probleme here",e)
} finnaly {
 //action C
 resources.close();
}

如果动作 A 和 be 和 int a = 0 一样原始,那么没有区别,但在像这样更复杂的情况下,finnaly 块有它的用法

Better look at this example, resources are allways closed even if I return or propagate the Excpetion up.

try {
 //action A
 return 0;
} catch (Exception e){
 //action C
 throw new Excpetion("Probleme here",e)
} finnaly {
 //action C
 resources.close();
}

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

拒绝两难 2024-11-10 23:31:02

来自 Sun 教程

注意:如果 JVM 在 try 时退出
或者catch代码正在执行,那么
finally 块将不会执行。
同样,如果线程执行
try 或 catch 代码被中断或
被杀死,finally 块不会
即使应用程序作为
整体仍在继续。

我不知道还有什么其他方法finally 块不会执行......

from the Sun Tutorials

Note: If the JVM exits while the try
or catch code is being executed, then
the finally block will not execute.
Likewise, if the thread executing the
try or catch code is interrupted or
killed, the finally block will not
execute even though the application as
a whole continues.

I don't know of any other ways the finally block wouldn't execute...

べ繥欢鉨o。 2024-11-10 23:31:02

如果 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.

时光磨忆 2024-11-10 23:31:02

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.

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