【发布时间】:2021-03-16 13:14:51
【问题描述】:
我有以下代码,我试图了解如何正确处理 catch 块中的异常,以便抛出两个异常(mainException 和 anotherException),所以在 anotherException 的情况下我们会这样做不要丢失来自mainException 的信息。
代码也很难闻——这种try-catch用法是某种反模式吗?有没有更好/正确的方法来处理这种情况?
try {
-some code-
} catch (RuntimeException mainException) {
try {
-another code-
} catch (Exception anotherException) {
throw anotherException;
} finally {
throw mainException;
}
}
【问题讨论】:
标签: java exception try-catch try-catch-finally