LoginSignup
0
0

More than 3 years have passed since last update.

try-with-resourcesのclose時例外

Posted at

public class Main {
    public static void main(String... args) throws Exception {
        try (Closable1 c1 = new Closable1(); Closable2 c2 = new Closable2()) {
            // none
        }
    }

    static class Closable1 implements AutoCloseable {
        @Override
        public void close() throws Exception {
            System.out.println("1 close.");
            throw new Exception("1 failed");
        }
    }

    static class Closable2 implements AutoCloseable {
        @Override
        public void close() throws Exception {
            System.out.println("2 close.");
            throw new Exception("2 failed");
        }
    }
}

std-in

2 close.
1 close.

std-error

Exception in thread "main" java.lang.Exception: 2 failed
    at com.company.Main$Closable2.close(Main.java:23)
    at com.company.Main.main(Main.java:8)
    Suppressed: java.lang.Exception: 1 failed
        at com.company.Main$Closable1.close(Main.java:15)
        at com.company.Main.main(Main.java:6)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0