使用简化的代码,我可以抛出Checked Exception而不会捕获(与uncheked相同)。从javadoc关于RuntimeException - 编译时检查例外。有没有办法阻止运行时的行为?
import java.io.IOException;
public class TestException {
static <E extends Exception>
void doThrow(final Exception e) throws E {
throw (E) e;
}
public static void main(String[] args) {
TestException.<RuntimeException>doThrow(
new IOException("Checked exception thrown")
);
}
}
答案 0 :(得分:2)
是,请将方法签名更正为:
static <E extends Exception> void doThrow(final E e) throws E {
throw e;
}