Java-generics-no-exception
提供:Dev Guides
Java Generics-例外なし
ジェネリッククラスは、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;
}
}