java對異常泛型的簡單實例
/**
- 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 tryi ng 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
- @author Eric
- */
public class ExceptionOfGeneric {
public static void main(String args[]) {
} }ProcessRunner<String, Process1Exception> pr1 = new ProcessRunner<String, Process1Exception>(); pr1.add(new ProcessImp1()); try { System.out.println(pr1.runnAlllProcess()); } catch (Process1Exception e) { e.printStackTrace(); } ProcessRunner<Integer, Process2Exception> pr2 = new ProcessRunner<Integer, Process2Exception>(); pr2.add(new ProcessImp2()); try { System.out.println(pr2.runnAlllProcess()); } catch (Process2Exception e) { e.printStackTrace(); }
interface Process<T, E extends Exception> { public void process(List<T> t) throws E; }
class Process1Exception extends Exception {}
class Process2Exception extends Exception {}
class ProcessRunner<T, E extends Exception> extends ArrayList<Process<T, E>> { List<T> runnAlllProcess() throws E { List<T> results = new ArrayList<T>(); for (Process<T, E> t : this) { // execute process may be throw variable type of exception t.process(results); } return results; } }
class ProcessImp1 implements Process<String, Process1Exception> {
public void process(List<String> strs) throws Process1Exception {
strs.add("HELLO");
}
}
class ProcessImp2 implements Process<Integer, Process2Exception> {
public void process(List<Integer> t) throws Process2Exception {
t.add(10);
}
}</pre>
本文由用戶 zt80 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!