通用类不允许直接或间接扩展Throwable
类。
//The generic class Box<T> may not subclass java.lang.Throwable
class Box<T> extends Exception {}
//The generic class Box<T> may not subclass java.lang.Throwable
class Box1<T> extends Throwable {}
在一个方法中,不允许捕获一个类型参数的实例,如下代码 -
public static <T extends Exception, J>
void execute(List<J> jobs) {
try {
for (J job : jobs){}
// compile-time error
//Cannot use the type parameter T in a catch block
} catch (T e) {
// ...
}
}
throws
子句中允许使用类型参数。
class Box<T extends Exception> {
private int t;
public void add(int t) throws T {
this.t = t;
}
public int get() {
return t;
}
}
上一篇:
Java泛型不能使用数组
下一篇:
Java泛型不能重载