Open
Description
参考 org.apache.commons.lang3.exception.ExceptionUtils
的做法:
public static <R> R rethrow(Throwable throwable) {
// claim that the typeErasure invocation throws a RuntimeException
return ExceptionUtils.<R, RuntimeException> typeErasure(throwable);
}
@SuppressWarnings("unchecked")
private static <R, T extends Throwable> R typeErasure(Throwable throwable) throws T {
throw (T) throwable;
}
测试:
public class Main {
public static void main(String[] args) {
try {
throw new Exception();
} catch (Exception e) {
ExceptionUtils.rethrow(e);
}
}
}
输出:
Exception in thread "main" java.lang.Exception
at com.github.bpazy.test.Main.main(Main.java:14)
Metadata
Metadata
Assignees
Labels
No labels