1 为何不能catch泛型变量?如:
- public static <T extends Throwable> void doWork(Class<T> t) {
- try {
- //do work
- }
- catch (T e) // ERROR--can't catch type variable
- {
- e.printStackTrace();
- }
- }
复制代码
2 但是可以throws泛型变量,为何?如:
- public static <T extends Throwable> void doWork(T t) throws T // OK
- {
- try {
- // do work
- }
- catch (Throwable e) {
- e.printStackTrace()
- }
- }
复制代码 |
|