我有一些代码可能会抛出已检查和运行时异常.
我想捕获已检查的异常并将其包装为运行时异常.但是如果抛出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)
想要更优雅的方式吗?