AutoCloseable close() 方法异常未被抑制
AutoCloseable close() method exception not getting suppressed
我正在 Java 8 中的一个简单应用程序中使用 try-with-resources。
我正在实施 AutoCloseable 接口并覆盖 close() 方法。
在我实现 AutoCloseable 的 class 之一中,我在 close() 方法中抛出一个异常,该异常将作为抑制异常工作。在我的主要方法中,我正在捕获异常。但是 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.
为什么关闭方法中的异常没有被抑制?
我想你必须再读一遍关于 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)
我正在 Java 8 中的一个简单应用程序中使用 try-with-resources。 我正在实施 AutoCloseable 接口并覆盖 close() 方法。 在我实现 AutoCloseable 的 class 之一中,我在 close() 方法中抛出一个异常,该异常将作为抑制异常工作。在我的主要方法中,我正在捕获异常。但是 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.
为什么关闭方法中的异常没有被抑制?
我想你必须再读一遍关于 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)