aspose file tools*
The moose likes Java in General and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception handling " Watch "Exception handling " New topic
Author

Exception handling

Jim Vamvakitis
Greenhorn

Joined: Apr 18, 2003
Posts: 4
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.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Pass a callback to whatever it is that does whatever you're doing.
One of these for example:


Tony Morris
Java Q&A (FAQ, Trivia)
bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
Could you provide details. I dont think this solution would be ideal. Maybe I don't understand, please elaborate.

Originally posted by Tony Morris:
Pass a callback to whatever it is that does whatever you're doing.
One of these for example:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Do you mean you haveor do you mean you don't know how to create a collection of exceptions, or what?
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306

interface ExceptionHandler
{
void handle(Throwable t) throws NullPointerException;
}


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



Another suggestion...

try
{
//run batch
run(); //collect exceptions while running
}
finally
{
ReleaseResources();
}

run()
{
try
{
//for each transaction
}
catch(Throwable t)
{
handleException();
}
}

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
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.

Perhaps an example:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...


"I'm not back." - Bill Harding, Twister
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Originally posted by Jim Yingst:
I believe Paul Clapham meant to direct his question to Jim Vamvakitis, not to Tony.
Yes, that's correct. Apologies for not being clear.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
OK sorry, my mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling