Meaningless Drivel is fun!
The moose likes OO, Patterns, UML and Refactoring and the fly likes handling errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "handling errors" Watch "handling errors" New topic

handling errors

Srinivas Ramgopal
Ranch Hand

Joined: Aug 06, 2006
Posts: 63
Hi all,

In case of a service development wherein the service could be called by other services in the workflow, is it okay to handle java.lang.Error and runtime exceptions inorder to handle the flow gracefully (though Sun does not recommend so, I have been reading few latest articles in web that recommend otherwise).
Thus I would like to know your views.

Thanks in advance for your valuable input and time.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

How would the callers of your service know that something went wrong? I would prefer to let them catch the exceptions if it is important to them.

Note that catching Error is even more dangerous than catching RuntimeException. Errors are things that hard to recover from.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd be inclined to catch as much as I could and throw application specific exceptions, but there are some errors that probably mean the JVM is so messed up that your attempt to throw something new won't work either.

What kind of environment are you running in? It's probably good to have some kind of service redundancy through load balancing and maybe even self healing restarts. In EJB land a service is just a thread within the container. If one dies, the container will likely start another.

In Forte 4GL (not Java) we had services that could automatically restart when they crashed and clients that could automatically retry. Crash, restart and retry could go on forever if the client is passing bad data that causes the crash, so even that has to have limits, eg only "n" retries.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: handling errors
It's not a secret anymore!