Skip to content

Instantly share code, notes, and snippets.

@megascus
Created July 6, 2017 04:20

Revisions

  1. megascus created this gist Jul 6, 2017.
    28 changes: 28 additions & 0 deletions CloseableTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    public class CloseableTest implements AutoCloseable {

    public static void main(String[] args) {

    try (CloseableTest test = new CloseableTest()) {
    test.throwException();;
    } catch (Exception e) {
    System.out.println("catch exception");
    System.out.println(e.getSuppressed()[0]); //close()メソッドの中で投げられた例外にcatch句でアクセスができる。
    }

    }

    CloseableTest() {
    System.out.println("new CloseableTest");
    }

    void throwException() throws Exception {
    System.out.println("throwException()");
    throw new Exception();
    }

    @Override
    public void close() throws Exception {
    System.out.println("close()");
    throw new Exception("surpressed");
    }
    }