未捕获的 RuntimeException 和 finally 子句:哪个先出现?

发布于 2024-12-17 16:56:51 字数 906 浏览 2 评论 0原文

RuntimeExceptiontry 块中抛出但未被捕获,而 finally 子句调用 System.exit()

public static void main(String[] args) {
    try {
        Integer.valueOf("NotANumber");
    } finally {
        System.out.println("finally");
        System.exit(0);
    }
}

输出为

finally

如果从finally 中删除System.exit(0),则输出为

finally
Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.valueOf(Integer.java:554)
    at exception.MyExcepTest.main(MyExcepTest.java:20)

其中 "finally" 可能出现在 NumberFormatException

有人能解释一下吗?

A RuntimeException is thrown in try block without being caught, while the finally clause invokes System.exit().

public static void main(String[] args) {
    try {
        Integer.valueOf("NotANumber");
    } finally {
        System.out.println("finally");
        System.exit(0);
    }
}

The output is

finally

If System.exit(0) is removed from finally, then the output is

finally
Exception in thread "main" java.lang.NumberFormatException: For input string: "NotANumber"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.valueOf(Integer.java:554)
    at exception.MyExcepTest.main(MyExcepTest.java:20)

Where "finally" may appears before, after or in between the meesage of NumberFormatException.

Can anybody explain it?

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

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

发布评论

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

评论(5

欢你一世 2024-12-24 16:56:51

finally 块肯定会在 main 方法退出之前执行,之后 JVM 会打印堆栈跟踪。

也许堆栈跟踪会打印到 System.err,并且两个流会以不可预测的方式在控制台输出中混合(因为它们基本上是同时生成的)。

当您也将“finally”打印到 System.err 时会发生什么?

The finally block will definitely be executed before the main method exits, and the stacktrace is printed by the JVM after that.

Maybe the stacktrace gets printed to System.err, and the two streams get mixed up in your console output in unpredictable ways (since they are produced basically simultaneously).

What happens when you print "finally" to System.err as well?

等风来 2024-12-24 16:56:51

问题是,当抛出异常时。JVM 首先执行带有finally 块内部的代码,然后在捕获异常时抛出异常,否则将抛出异常并终止线程。
所以当 System.exit(0) 出现在finally块中时,它会立即终止线程,这样JVM就没有机会抛出异常。
所以输出只是“最后

The thing is, when there is an exception thrown.. The JVM 1st execute code with inside finally block and then throw the exception if catched or it will throw the exception and terminate the thread.
so here when System.exit(0) is present in the finally block it terminate the thread immediately so the JVM doesnt get chance to throw the exception.
so the out put is just the "finally
"

2024-12-24 16:56:51

最后块总是被执行。这是由语言保证的。如果 try 块成功终止或抛出任何异常,则会执行它。

有检查异常和非检查异常。对于未经检查的异常(运行时和错误),您不必编写 catch 块。但所有异常都会被 JVM 捕获并打印堆栈跟踪。当您的finally块终止应用程序时,它没有机会打印堆栈跟踪,因此您看不到它。

一般来说,在finally块中退出程序是不好的,因为即使你的代码成功运行它也会退出。更一般地说,finally 块通常用于清理,例如关闭文件、套接字等,而不是用于更复杂的业务逻辑。

Finally block is executed always. It is guaranteed by the language. It is executed if you try block is terminated successfully or if any exception is thrown.

There are checked and unchecked exceptions. For unchecked exceptions (Runtime and Errors) you do not have to write catch block. But all exceptions are caught by JVM that prints the stacktrace. When your finally block terminates application it does not have the chance to print the stacktrace, so you do not see it.

Generally exiting program in finally block is bad because it will exit even if your code runs successfully. And more generally finally block is typically needed for cleanup like closing files, sockets etc and not for more complicated business logic.

忆离笙 2024-12-24 16:56:51

有两个块可以与 try 一起使用,它们是 catch 和 finally。

当抛出任何运行时异常时(finally之前)都会执行catch块,而无论是否抛出异常,finally块都会在最后执行。

因此,如果您想对抛出的异常执行某些操作,则可以将其放入 catch(Excepion e) 块中。

您看到的是 JVM 的职责是在终止程序执行之前执行finally 块中写入的内容。

当程序终止时,它默认显示抛出异常的跟踪。

there are two blocks that we can use with try those are catch and finally.

catch block is executed when any RunTime exception is thrown (before finally) and finally block is executed in the end, irrespective of exception is thrown or not.

so if you want to do something on exception being thrown then you can put that in catch(Excepion e) block.

And what are you seeing is the duty of JVM to perform what ever is written in the finally block before terminating the program execution.

And when program is terminated it by default shows you the trace of the exception thrown.

北城孤痞 2024-12-24 16:56:51

即使在try块中存在return语句,finally方法也总是会被执行,但在某些情况下,在try块中抛出错误(运行时内存)时,不能保证finally块完全执行。

在您的情况下,finally 块始终执行,并且由 JVM 的 main 方法抛出异常,因为您没有处理异常。

Finally method always will be executed even in case of return statement in try block ,but in some cases where throwing Errors(Run time memory) in try block there is no guarantee finally block executed completely.

In your case finally block always executed and exception throws by main method from the JVM since you are not handle the exception.

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