Java泛型(10):泛型与异常
由于擦除的原因,将泛型运用于异常是非常受限的。catch语句不能捕获泛型类型的异常,因为在编译期和运行期都必须知道异常的确切类型。但是,类型参数可能会在一个方法的throw子句中用到。这使得你可以编写随检查型异常的类型而发生变化的泛型代码。请看下面的例子。
1 interface Processor<T,E extends Exception> { 2 void process(List<T> resultCollector) throws E; 3 } 4 5 class ProcessRunner<T,E extends Exception> extends ArrayList<Processor<T,E>> { 6 List<T> processAll() throws E { 7 List<T> resultCollector = new ArrayList<T>(); 8 for(Processor<T,E> processor : this) 9 processor.process(resultCollector); 10 return resultCollector; 11 } 12 } 13 14 class Failure1 extends Exception {} 15 16 class Processor1 implements Processor<String,Failure1> { 17 static int count = 3; 18 public void process(List<String> resultCollector) throws Failure1 { 19 if(count-- > 1) 20 resultCollector.add("Hep!"); 21 else 22 resultCollector.add("Ho!"); 23 if(count < 0) 24 throw new Failure1(); 25 } 26 } 27 28 class Failure2 extends Exception {} 29 30 class Processor2 implements Processor<Integer,Failure2> { 31 static int count = 2; 32 public void process(List<Integer> resultCollector) throws Failure2 { 33 if(count-- == 0) 34 resultCollector.add(47); 35 else { 36 resultCollector.add(11); 37 } 38 if(count < 0) 39 throw new Failure2(); 40 } 41 } 42 43 public class ThrowGenericException { 44 public static void main(String[] args) { 45 ProcessRunner<String,Failure1> runner = new ProcessRunner<>(); 46 for(int i = 0; i < 3; i++) 47 runner.add(new Processor1()); 48 try { 49 System.out.println(runner.processAll()); // [Hep!, Hep!, Ho!] 50 } catch(Failure1 e) { 51 System.out.println(e); 52 } 53 ProcessRunner<Integer,Failure2> runner2 = new ProcessRunner<>(); 54 for(int i = 0; i < 3; i++) 55 runner2.add(new Processor2()); 56 try { 57 System.out.println(runner2.processAll()); 58 } catch(Failure2 e) { 59 System.out.println(e); // chapter15.class14.Failure2 60 } 61 } 62 }