Thumbs up for suppressable exceptions in JDK 1.7
Dear All
Was just playing around with suppressable exceptions
in JDK 1.7. This looks like a great improvement for
bug hunting!
Best Regards
Manually Suppressed:
------------------
public class TestSuppressed {
public static void main(String[] args) throws Exception {
try {
throw new IllegalArgumentException("x");
} catch (IllegalArgumentException x) {
x.addSuppressed(new IllegalArgumentException("y"));
throw x;
}
}
}
gives:
Exception in thread "main" java.lang.IllegalArgumentException: x
at TestSuppressed.main(TestSuppressed.java:12)
Suppressed: java.lang.IllegalArgumentException: y
at TestSuppressed.main(TestSuppressed.java:14)
... 5 more
Automatically Suppressed:
---------------------
public class TestSuppressed implements AutoCloseable {
public static void main(String[] args) throws Exception {
try (TestSuppressed ts=new TestSuppressed()) {
throw new IllegalArgumentException("x");
}
}
public void close() throws Exception {
throw new IllegalArgumentException("y");
}
}
gives:
Exception in thread "main" java.lang.IllegalArgumentException: x
at TestSuppressed.main(TestSuppressed.java:9)
Suppressed: java.lang.IllegalArgumentException: y
at TestSuppressed.close(TestSuppressed.java:20)
at TestSuppressed.main(TestSuppressed.java:10)
... 5 more