Is the close method on a try-with-resources idiom not called if a constructor throws an exception?

半世苍凉 提交于 2021-01-22 04:51:23

问题


I have a base class Base and a child class Child which extends it. Base implements java.lang.AutoCloseable.

Let's suppose that the constructor for Child throws a Foo.

Now consider

try (Base c = new Child()){
    /*Some code*/
} catch (final Foo e){
    /*Some more code*/
}

Is the Base#close method called if the exception is thrown? It is not on my machine, but is this something that the JLS has standardised?


回答1:


Yes, close won't be called. This is specified in the JLS section 14.20.3:

Resources are initialized in left-to-right order. If a resource fails to initialize (that is, its initializer expression throws an exception), then all resources initialized so far by the try-with-resources statement are closed. If all resources initialize successfully, the try block executes as normal and then all non-null resources of the try-with-resources statement are closed.

Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.

In this case, an exception is thrown in the constructor so the resource is not initialized to a non-null value. Hence, the close method isn't called.




回答2:


close will not be called. It wouldn't make sense to call it, since you don't have a fully-constructed object to close, and in similar calls, you might not have even entered a constructor:

try (Base b = makeBase()) {
    ...
}

where makeBase is

Base makeBase() {
    throw new RuntimeException();
}


来源:https://stackoverflow.com/questions/34678538/is-the-close-method-on-a-try-with-resources-idiom-not-called-if-a-constructor-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!