我希望下面的代码示例编译失败(main方法中的Unhandled exception),但它在Java 1.8中成功编译。 <E extends Throwable>
如何成为未经检查的例外?
public class Test {
public static void main(String[] args) {
throwAsUnchecked(new Exception("Testing new checked exception"));
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAsUnchecked(Exception exception) throws E {
throw (E) exception;
}
}