相关疑难解决方法(0)

如何包装已检查的异常但在Java中保留原始运行时异常

我有一些代码可能会抛出已检查和运行时异常.

我想捕获已检查的异常并将其包装为运行时异常.但是如果抛出RuntimeException,我不必将它包装起来,因为它已经是运行时异常.

我的解决方案有点开销,并不"整洁":

try {
  // some code that can throw both checked and runtime exception
} catch (RuntimeException e) {
  throw e;
} catch (Exception e) {
  throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)

想要更优雅的方式吗?

java runtimeexception checked-exceptions

25
推荐指数
3
解决办法
6640
查看次数