Java 8 无法抑制AutoCloseable close()方法异常
我正在使用Java 8的一个简单应用程序中的try with resources。 我正在实现AutoCloseable接口,而不是close()方法。 在我的一个实现AutoCloseable的类中,我在close()方法中抛出了一个异常,该异常将作为被抑制的异常工作。在我的主要方法中,我捕获了异常。但是close方法的异常并没有被抑制。当捕获到一般异常时,该异常将被捕获到catch块中 这是我的密码:Java 8 无法抑制AutoCloseable close()方法异常,java-8,try-with-resources,Java 8,Try With Resources,我正在使用Java 8的一个简单应用程序中的try with resources。 我正在实现AutoCloseable接口,而不是close()方法。 在我的一个实现AutoCloseable的类中,我在close()方法中抛出了一个异常,该异常将作为被抑制的异常工作。在我的主要方法中,我捕获了异常。但是close方法的异常并没有被抑制。当捕获到一般异常时,该异常将被捕获到catch块中 这是我的密码: public class TryWithResourcesExample { p
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方法中的异常没有被抑制?我想你必须再次阅读这篇文章 只有当存在多个异常时,才会抑制异常。此外,还指定了抑制异常的时间和顺序,如本例所示:
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)
我想你得再读一遍这篇文章 只有当存在多个异常时,才会抑制异常。此外,还指定了抑制异常的时间和顺序,如本例所示:
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)
不要把“抑制”和“吞咽”混为一谈不要把“抑制”和“吞咽”混为一谈