What is the output of the following?
public class Main { static class MyResource implements AutoCloseable { public void close() { throw new RuntimeException("fail"); }/*from w w w. j a v a 2 s . c o m*/ } public static void main(String[] args) { try (MyResource walk1 = new MyResource(); MyResource walk2 = new MyResource();) { throw new RuntimeException("fail2"); } catch (Exception e) { System.out.println(e.getMessage() + " " + e.getSuppressed().length); } } }
C.
The exception inside the try block becomes the primary exception since it is thrown first.
Then two suppressed exceptions are added to it when trying to close the AutoCloseable resources.