Results 1 to 1 of 1
Thread: Parameterized Exceptions
- 06-05-2010, 10:01 AM #1
Parameterized Exceptions
I was reading about "Exceptions and Java Generics" lately . I use
"Thinking in Java " by Bruce Eckel for learning java and there in the
book Bruce gave me this following question to work out . But the thing
is I am not able to decipher the question itself . Therefore it will
be nice if anyone here can help me to understand this question .
The Question :
==========
Add a second parameterized exception to Processor class and
demonstrate that the exceptions can vary independently .
==========
The Code the accompanied the question .
Thank you for your help .Java Code:package generics; 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); } } } ///:~
--
rsiddharthLover of Freedom and Simplicity .
http://rsiddharth.wordpress.com/
Similar Threads
-
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM -
The type Class is not generic; it cannot be parameterized with arguments <T>
By new_2_java in forum Advanced JavaReplies: 1Last Post: 05-16-2008, 11:06 PM -
Will struts work for parameterized message in internationlization
By felixtfelix in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-04-2008, 01:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks