我有一些代码可能会抛出已检查异常和运行时异常。
我想捕获已检查异常并将其包装为运行时异常。但如果抛出RuntimeException,我不必包装它,因为它已经是运行时异常了。
我现在有一个解决方案,但有一些额外的开销,而且不太“整洁”:
try {
// some code that can throw both checked and runtime exception
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
有没有更优雅的方法?