Java suppressed exceptions

public class Main {
    public static void main(String[] args) {
        doSomething();
    }
    public static void doSomething(){
        try(MyAutoCloseable ac = new MyAutoCloseable()){
            ac.saySomething();
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
        }
    }
}
public class MyAutoCloseable implements AutoCloseable {
    public void saySomething() throws IOException{
        throw new IOException("Exception from saySomething");
    }
    @Override
    public void close() throws IOException {
        throw new IOException("Exception from close");
    }
}

try with resources 语法中遇到嵌套的throw,里面的异常就是被抑制的异常,suppressed exception,我们可以看一眼反编译的代码,可以发现编译器做了很多工作

public static void doSomething() {
    try {
        MyAutoCloseable ac = new MyAutoCloseable();

        try {
            ac.saySomething();
        } catch (Throwable var4) {
            try {
                ac.close();
            } catch (Throwable var3) {
                var4.addSuppressed(var3);
            }

            throw var4;
        }

        ac.close();
    } catch (IOException var5) {
        PrintStream var10000 = System.out;
        String var10001 = var5.getClass().getSimpleName();
        var10000.println(var10001 + " - " + var5.getMessage());
    }

}

所以为了看到完整的异常,我们可以使用getSuppressed方法

public static void doSomething(){
    try(MyAutoCloseable ac = new MyAutoCloseable()){
        ac.saySomething();
    } catch (IOException e) {
        System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
        for (Throwable t:e.getSuppressed()){
            System.out.println("Suppressed: " + t.getMessage());
        }
    }
}

看一个更复杂的例子

public static void doTryWithResourcesMulti() {
    char[] buff = new char[8];
    int length;
    try (Reader reader = Helper.openReader("data/file.txt");
         Writer writer = Helper.openWriter("data/file2.txt")) {
        while ((length = reader.read(buff)) >= 0) {
            System.out.println("\nlength: " + length);
//                for (int i = 0; i < length; i++) System.out.print(buff[i]);
            writer.write(buff, 0, length);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void doTryWithResourcesMulti() {
    char[] buff = new char[8];

    try {
        Reader reader = Helper.openReader("data/file.txt");

        try {
            Writer writer = Helper.openWriter("data/file2.txt");

            int length;
            try {
                while((length = reader.read(buff)) >= 0) {
                    System.out.println("\nlength: " + length);
                    writer.write(buff, 0, length);
                }
            } catch (Throwable var8) {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (Throwable var7) {
                        var8.addSuppressed(var7);
                    }
                }

                throw var8;
            }

            if (writer != null) {
                writer.close();
            }
        } catch (Throwable var9) {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Throwable var6) {
                    var9.addSuppressed(var6);
                }
            }

            throw var9;
        }

        if (reader != null) {
            reader.close();
        }
    } catch (IOException var10) {
        var10.printStackTrace();
    }

}

Leave a comment

Your email address will not be published. Required fields are marked *