This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Could somoene help by showing me how to handle a collection of exceptions, I have a JMS service that I do batch call of 50 and each one that does not go through an exception message is thrown. I have to execute the batch process and at the end I throw an exception that has the global exceptions caught. Any idea or help would be appreciated. Thanks.
By using throws can you convert an unchecked exception to checked? You can't. Wherever there is a '.' there are chances of NullPointerException. Unchecked exceptions are easy to prevent than "overcome after letting it happen"
I will suggest (Below is something very crude, you may refine it for maintainability and flexibility)
exceptionCollection = ..; //prefers an ordered collection or FIFO so that exceptions can be chnologically handled. try { //run batch run(); //collect exceptions while running } finally { handleExceptions(); }
run() { try { //for each } catch(Throwable t) { exceptionCollection.add(t); if (!canContinue(t)) { releaseResources(); terminate(); } } }
handleException() { for(Iterator iter= exceptionCollection.iterator;...;..) { Throwable th = iter.next(); log(ERROR, th); //or whatever you want to do } }
While analyzing exceptions on a batch you may find time at which exception occured is a factor. Ex. An external system may be down at 8:00PM. In this case your Exception could mark the time of ExceptionOccurance too
handleException() { log(ERROR, th); //or whatever you want to do }
I like the second solution better than first one. In the second one if a deadlock happens or jvm terminates (ex: out of memmory), still I got some exceptions logged and predict the state of data from the log. [ December 19, 2005: Message edited by: jiju ka ]
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
And so we digress...
Do you mean you have
or do you mean you don't know how to create a collection of exceptions, or what?
No I do not mean that, and yes I know how to create a collection of exceptions. I would certainly not recommend passing a java.util.Collection to any method, ever, but that's another story.
By using throws can you convert an unchecked exception to checked? You can't. Wherever there is a '.' there are chances of NullPointerException. Unchecked exceptions are easy to prevent than "overcome after letting it happen"
No, the throws clause is merely part of the contract. All methods that accept one or more reference type should throw NullPointerException - this is the optimal workaround to the flawed existence of null (even the Gods that the sheep faithfully follow will agree with this, so there should be a lot of literature on it by now).
To the point, all you are doing is passing a single instance to "whatever it is that might throw an exception" so that it can be called back on in the event of an exception. The downside is that you lose definite assignment semantics - this can remedied by the handle method returning a java.lang.RuntimeException and the implementation that calls back can declare to throw it.
I believe Paul Clapham meant to direct his question to Jim Vamvakitis, not to Tony. At least, Paul's question matched what I was wondering about Jim V's post. Might be useful to get Jim V to clarify what he really meant...