了解 Try/Finally 抑制异常
Understanding Try/Finally Suppressed Exceptions
我正在学习异常,我发现您可以抑制异常。
我在 Whosebug 上阅读了很多示例,但它们仍然无法正常工作,因为它们在“try/finally”案例中应该如此:
public class MultipleExceptionsExample {
static class IOManip implements Closeable{
@Override
public void close() {
throw new RuntimeException("from IOManip.close");
}
}
public static void main(String[] args) {
try(IOManip ioManip = new IOManip()){
throw new RuntimeException("from try!");
}catch(Exception e){
throw new RuntimeException("from catch!");
}finally{
throw new RuntimeException("from finally!");
}
}
}
正如许多人所解释的那样,我应该得到所有的行:“java.lang.RuntimeException:从最后!” (是的,我知道)
删除 finally 块我应该得到:“java.lang.RuntimeException: from catch!” (是的,我知道)
删除 catch 块我应该得到:
Exception in thread "main" java.lang.RuntimeException: from try!
Suppressed: java.lang.RuntimeException: from IOManip.close
而且我从不这样做!为什么?我错过了什么?
通过删除 catch 块,我应该会看到 try 消息,但我得到的却是:
Exception in thread "main" java.lang.RuntimeException: from finally!
at it.core.MultipleExceptionsExample.main(MultipleExceptionsExample.java:18)
非常感谢。
A return 或 finally
块中的抛出会导致任何其他 returned(或抛出)结果被忽略。
不要在 finally
块中 return
或 throw
,除非你 真的 知道你在做什么。
需要注意的是 Java Throwables 有一个“抑制”异常的概念:你可以调用 addSuppressed
on a Throwable
that you have caught; this is done automatically with a try-with-resources 块,如果在正文中发生异常 and 同时关闭资源;但这不会发生在 finally
块中。
这是因为finally
块总是会执行。是否捕获到异常并不重要。要更好地理解它,请尝试以下代码:
public static void main(String[] args) {
try (IOManip ioManip = new IOManip()) {
System.out.println("Try!");
throw new RuntimeException("from try!");
} finally {
System.out.println("Finally!");
}
}
请记住,return 或从 finally 块中抛出不是一个好习惯,您应该避免这样做。
我正在学习异常,我发现您可以抑制异常。 我在 Whosebug 上阅读了很多示例,但它们仍然无法正常工作,因为它们在“try/finally”案例中应该如此:
public class MultipleExceptionsExample {
static class IOManip implements Closeable{
@Override
public void close() {
throw new RuntimeException("from IOManip.close");
}
}
public static void main(String[] args) {
try(IOManip ioManip = new IOManip()){
throw new RuntimeException("from try!");
}catch(Exception e){
throw new RuntimeException("from catch!");
}finally{
throw new RuntimeException("from finally!");
}
}
}
正如许多人所解释的那样,我应该得到所有的行:“java.lang.RuntimeException:从最后!” (是的,我知道)
删除 finally 块我应该得到:“java.lang.RuntimeException: from catch!” (是的,我知道)
删除 catch 块我应该得到:
Exception in thread "main" java.lang.RuntimeException: from try!
Suppressed: java.lang.RuntimeException: from IOManip.close
而且我从不这样做!为什么?我错过了什么?
通过删除 catch 块,我应该会看到 try 消息,但我得到的却是:
Exception in thread "main" java.lang.RuntimeException: from finally!
at it.core.MultipleExceptionsExample.main(MultipleExceptionsExample.java:18)
非常感谢。
A return 或 finally
块中的抛出会导致任何其他 returned(或抛出)结果被忽略。
不要在 finally
块中 return
或 throw
,除非你 真的 知道你在做什么。
需要注意的是 Java Throwables 有一个“抑制”异常的概念:你可以调用 addSuppressed
on a Throwable
that you have caught; this is done automatically with a try-with-resources 块,如果在正文中发生异常 and 同时关闭资源;但这不会发生在 finally
块中。
这是因为finally
块总是会执行。是否捕获到异常并不重要。要更好地理解它,请尝试以下代码:
public static void main(String[] args) {
try (IOManip ioManip = new IOManip()) {
System.out.println("Try!");
throw new RuntimeException("from try!");
} finally {
System.out.println("Finally!");
}
}
请记住,return 或从 finally 块中抛出不是一个好习惯,您应该避免这样做。