What is a suppressed exception?

后端 未结 8 1585
借酒劲吻你
借酒劲吻你 2020-11-28 04:51

A comment (by user soc) on an answer to a question about tail call optimisation mentioned that Java 7 has a new feature called \"suppressed exceptions\", because of \"the ad

相关标签:
8条回答
  • 2020-11-28 05:27

    Suppressed exceptions are additional exceptions that occur within a try-with-resources statement (introduced in Java 7) when AutoCloseable resources are closed. Because multiple exceptions may occur while closing AutoCloseable resources, additional exceptions are attached to a primary exception as suppressed exceptions.

    Looking at the bytecode of a piece of try-with-resources sample code, standard JVM exception handlers are used to accommodate the try-with-resources semantics.

    0 讨论(0)
  • 2020-11-28 05:27

    You can suppress Exceptions in Java 6 as well (a little trickery involved),

    I created a utility that transparently handles suppressing exception in Java 1.6 and Java 1.7. You can find the implementation here

    All you need is to call:

    public static <T extends Throwable> T suppress(final T t, final Throwable suppressed) 
    

    to supress a exception, and

    public static Throwable [] getSuppressed(final Throwable t) {
    

    to get the suppressed exceptions of a Exception, in case anybody still uses Java 1.6

    0 讨论(0)
提交回复
热议问题