【问题标题】:AutoCloseable close() method exception not getting suppressedAutoCloseable close() 方法异常没有被抑制
【发布时间】:2016-12-08 10:27:15
【问题描述】:

我正在 Java 8 中的一个简单应用程序中使用 try-with-resources。 我正在实现 AutoCloseable 接口并覆盖 close() 方法。 在我的一个实现 AutoCloseable 的类中,我在 close() 方法中抛出了一个异常,该方法将作为 Suppressed Exception 工作。在我的主要方法中,我发现了异常。但是 close 方法的异常并没有被抑制。由于捕获了一般异常,因此该异常被捕获在 catch 块中。

这是我的代码:

public class TryWithResourcesExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (Lion lion = new Lion(); Tiger tiger = new Tiger()) {

            lion.hunt();
            tiger.hunt();

        } catch (Exception e) {
            System.out.println("Got Simple Exception = "+e);
            for(Throwable t: e.getSuppressed())
            {
                System.out.println("Got Suppressed Exception = "+t);
                t.printStackTrace();
            }
        } finally {
            System.out.println("Finally.");
        }
    }

}

class Tiger implements AutoCloseable {
    public Tiger() {
        System.out.println("TIGER is OPEN in the wild.");
    };

    public void hunt() throws Exception {
        //throw new Exception("DeerNotFound says Tiger!");
    }

    public void close() throws Exception {
        System.out.println("Tiger is CLOSED in the cage.");
    }
}

class Lion implements AutoCloseable {

    public Lion() {
        System.out.println("LION is OPEN in the wild.");
    }

    public void hunt() throws Exception {
        //throw new Exception("DeerNotFound says Lion!");
    }

    public void close() throws Exception {
        System.out.println("LION is CLOSED in the cage.");
        throw new Exception("Lion Unable to close the cage!");
    }
}

这是我的输出:

LION is OPEN in the wild.
TIGER is OPEN in the wild.
Tiger is CLOSED in the cage.
LION is CLOSED in the cage.
Got Simple Exception = java.lang.Exception: Lion Unable to close the cage!
Finally.

为什么 close 方法中的异常没有被抑制?

【问题讨论】:

  • 不要将“抑制”与“吞咽”混为一谈

标签: java-8 try-with-resources


【解决方案1】:

我认为您必须再次阅读有关try-with-resource 的文章。

仅当有多个异常时才会抑制异常。还指定了一个顺序,如本例所示,何时以及哪些异常被抑制:

public class Test {
  public static void main(String... args) {
    try (ExceptionResource test = new ExceptionResource()) {
      test.work();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class ExceptionResource implements AutoCloseable {

  public void work() throws Exception {
    throw new Exception("Thrown in 'run'");
  }

  @Override
  public void close() throws Exception {
    throw new Exception("Thrown in 'close'");
  }
}

结果:

java.lang.Exception: Thrown in 'run'
at test.ExceptionResource.work(Test.java:16)
at test.Test.main(Test.java:6)
Suppressed: java.lang.Exception: Thrown in 'close'
    at test.ExceptionResource.close(Test.java:21)
    at test.Test.main(Test.java:7)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2018-12-08
    • 2020-02-02
    • 2011-07-18
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多