English 中文(简体)
选择不停和最后条款:首先出现什么?
原标题:Uncaught RuntimeException and finally clause: which comes first?

缩略语 条款援引<代码>System.exit()。

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

产出

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)

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

Can anybody explain it?

最佳回答

在主要方法出走之前,最终将肯定会执行,而后由科索沃信托局印刷。

也许, st子被印刷到系统手里,两流以不可预测的方式(因为基本上同时生产)在我们的ole果产出中出现混合。

当你“最终”印刷到系统时,情况如何?

问题回答

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 "

最后,路障总是被执行。 它得到语言的保障。 如果你试图成功终止封锁,或排除任何例外,就处决了。

检查并不受制约的例外。 对于不受限制的例外情况(Runtime和Errors),你不必写上渔获块。 但是,所有的例外情况都由印刷 st子的科索沃信托机构控制。 当您最终终止申请时,它没有机会印刷插头,因此你看不到。

一般来说,最后放逐方案是坏的,因为即使你的法典成功运行,该方案也会退出。 更一般而言,清理工作,如关闭档案、备案等,而不是更复杂的业务逻辑,都需要有更最终的障碍。

我们可以使用两个区块来审判这些区块,最后是 catch。

任何停泊时间例外被推翻(在最后之前)后,最终将执行,而不论有无例外。

如果你想要做一些例外,那么你就可以把这种事情 block在渔获(Excepion e)块中。

你看到的是,在终止方案执行之前,核查和核查机制有义务履行在最后部分写到的内容。

当方案因违约而终止时,就会发现所放弃的例外情况。

最后,即便在审判组的返回陈述中,也总是会执行这种方法,但在有些情况下,在审判区扔出Errors(Run时记忆)时,没有最终被彻底执行的保障。

在您的案例中,由于你没有处理这一例外情况,最终将永远搁置起来,并且以主要方法从核查和核查机制中排除例外情况。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签