简体   繁体   中英

Two close methods in try-with-resources

I've got the following code:

try(Socket s1=new Socket(...); Socket s2=new Socket(...);)
{
  some logic...
}

As I understood, Java closes s2 first and then s1 . What will happen, if s2.close() throws exception? Will Java try to close s1 after previous failure?

Quoting JLS Sec 14.20.3 (emphasis mine):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM