首页 > 解决方案 > 如何显示 catch 和 finally 的异常?

问题描述

让我们考虑以下代码:

try {
    throw new Exception("from try")
} catch (Exception e) {
    throw new Exception("from catch")
} finally {
    throw new Exception("from finally")
}

它给:

Exception thrown
java.lang.Exception: from finally
<...>

所以看起来finally在catch之前执行并终止了执行流程。

如果想同时看到这两个例外,我该怎么办?

标签: javagroovy

解决方案


所以看起来 finally 在 catch 之前执行并终止了执行流程。

这是不正确的。 finally在对应的之后执行catch,而不是之前。问题是您的catch块执行,然后finally保证块执行。


推荐阅读