Exceptions
Because of erasure, the use of generics with exceptions is extremely limited. A catch clause cannot catch an exception of a generic type, because the exact type of the exception must be known at both compile time and run time. Also,a generic class can't directly or indirectly inherit from Throwable (this further prevents you from trying to define generic exceptions that can't be caught).
However, type parameters may be used in the throws clause of a method declaration. This allows you to write generic code that varies with the type of a checked exception:
//: generics/ThrowGenericException.java import java.util.*; interface Processor<T,E extends Exception> { void process(List<T> resultCollector) throws E; } class ProcessRunner<T,E extends Exception> extends ArrayList<Processor<T,E>> { List<T> processAll() throws E { List<T> resultCollector = new ArrayList<T>(); for(Processor<T,E> processor : this) processor.process(resultCollector); return resultCollector; } } class Failure1 extends Exception {} class Processor1 implements Processor<String,Failure1> { static int count = 3; public void process(List<String> resultCollector) throws Failure1 { if(count-- > 1) resultCollector.add("Hep!"); else resultCollector.add("Ho!"); if(count < 0) throw new Failure1(); } } class Failure2 extends Exception {} class Processor2 implements Processor<Integer,Failure2> { static int count = 2; public void process(List<Integer> resultCollector) throws Failure2 { if(count-- == 0) resultCollector.add(47); else { resultCollector.add(11); } if(count < 0) throw new Failure2(); } } public class ThrowGenericException { public static void main(String[] args) { ProcessRunner<String,Failure1> runner = new ProcessRunner<String,Failure1>(); for(int i = 0; i < 3; i++) runner.add(new Processor1()); try { System.out.println(runner.processAll()); } catch(Failure1 e) { System.out.println(e); } ProcessRunner<Integer,Failure2> runner2 = new ProcessRunner<Integer,Failure2>(); for(int i = 0; i < 3; i++) runner2.add(new Processor2()); try { System.out.println(runner2.processAll()); } catch(Failure2 e) { System.out.println(e); } } } ///:~
A Processor performs a process( ) and may throw an exception of type E. The result of the process( ) is stored in the List<T> resultCollector (this is called a collecting parameter). A ProcessRunner has a processAll( ) method that executes every Process object that it holds, and returns the resultCollector.
If you could not parameterize the exceptions that are thrown, you would be unable to write this code generically because of the checked exceptions.
相关推荐
Generics_in_the_Java_Programming_Language的中文译文
Java.Generics.and.Collections.Oct.2006.pdf 完美清晰正版
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版.
英文资源,_Java_Generics_and_Collections
Java Generics and Collections. 超清英文pdf,针对泛型和集合的解析
NULL 博文链接:https://wuaner.iteye.com/blog/907980
Java Generics and Collections
Thinking in Java Fourth Edition Bruce Eckel 以下目录 是直接从pdf 中复制的: What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4 ...
Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................
Java generics and collections
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版
找了好久都只有Java泛型与集合这本书的chm版本,现在终于找到了pdf版本,分享一下
Java 的 Generics 简介。入门资料!
Java Generics Cheatsheet, your convenient way to check and learn!
The second edition of Programming in Java confirms to Java Standard Edition 7, the latest release since Oracle took over Sun Microsystems. It is significant in the sense that the last update was six ...
Java Generics and Collections.chm Java语言基础部分 必学知识。
Updates to Java 5.0 include new sections on generics and other Java 5.0 features, and revised code fragments, examples, and case studies to conform to Java 5.0. Hundreds of exercises, including many...