我有两种方法:Java泛型与异常产生编译时错误
private static <T extends Throwable> void methodWithExceptionGeneric(final T t) throws T {
throw t;
}
private static void methodWithExceptionNonGeneric(final Throwable t) throws Throwable {
throw t;
}
当我调用这些方法如下所示:
methodWithExceptionGeneric(new IllegalArgumentException());
methodWithExceptionNonGeneric(new IllegalArgumentException()); // compile time error
我得到的非泛型方法编译时错误说我的主方法中有一个未处理的异常,我需要声明一个throws语句或捕获异常。
我的问题是:为什么它只是抱怨非泛型方法?在我看来,泛型方法也抛出异常,所以不应该被处理呢?
你知道关于检查和未检查的异常吗?什么样的'IllegalArgumentException'? –
@SotiriosDelimanolis我不知道 – Ogen
@Ogen然后谷歌,检查文档:http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html,看到它扩展了一个'java.lang.RuntimeException' – alfasin