• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please clear this.

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

Regards,

Shrawan
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for this.

Regards,

Shrawan
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic