的java代码:
public static <T extends Throwable> void checkNotNull(Object value, String name, Class<T> exceptionClass) throws T, SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
if (value==null)
throw ExceptionHelper.constructException(exceptionClass, name + " should not be null");
}
static <T extends Throwable> T constructException(java.lang.Class<T> exceptionClass, String message) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<T> constructor = exceptionClass.getConstructor(String.class);
T result = constructor.newInstance(message);
return result;
}
junit代码:
@Test
public void testCheckNotNull() {
try {
ValidationUtility.checkNotNull(null, "valuename", exceptionClass);
} catch (T e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
然后编译器说:不能在catch块中使用类型参数T
那么如何解决这个问题呢?