wood burning stoves 2.0*
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

Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi all,

Please clear this.

Why is it not advisable to catch type Exception in try catch block.

Regards,

Shrawan
John Kelty
Ranch Hand

Joined: Aug 08, 2006
Posts: 33
In general, you should try to catch more specific exceptions, so that you can handle the exception in a more specific manner, particular to the error at hand. For example, if you catch an IOException, depending on your application, you could maybe prompt the user to try a new file name or close down any applications using the file in question. With just an Exception handler, you wouldn�t know if that was the problem, or if there was a null pointer exception somewhere.

And if you catch a specific exception rather than a more general one, you can often allow the more general exceptions (everything but IOException in my example) to propagate up the call stack to an exception handler elsewhere that is more suited for whatever that exception is.

That said, it�s not always bad to have a catch of Exception, as long as it is the last catch block listed. We do that frequently in our GUI code, as a last level of defense against exceptions that slipped through all the other exception handlers. We have to handle them very generically, but it does prevent us from crashing part of the application because of a null pointer exception.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is not the standard advice, but I don't bother with distinguishing exception types unless I'm really going to handle them differently. I dislike this kind of thing which I see fairly often:

This communicates not one more bit of information than

If someday in the future you decide to handle one exception differently it's just as easy to add the catch clause then as it is to code it now.


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
Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi All,

Thanks for this.

Regards,

Shrawan
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
But Stan, your first code example only loses the stack trace for the three explicitly named exceptions. Depending what they are, maybe that's fine; maybe you don't need the stack trace. However the replacement you recommend will lose the stack trace for any exception that may occur within the try. Like, say, a NullPointerException. Which now becomes much harder to track down.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Right. Catching all exceptions takes some extra care. In real life I'd print stack traces, too. And I'd occasionally catch something I really shouldn't try to recover from. That hasn't bit me yet, but doesn't mean it won't one day.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Exception Handling
 
Similar Threads
Doubt in Exception Handling.
What are the below exception Statement can any one explain them?
NaturalLanguageMultiply: Exceptions
Resume of program
Can you throw too many exceptions?