• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

catch Throwable

 
Michael Imhof
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of java programmers just use following code for exception handling

I would like to hear your opinion about this overall exception catching.
In my opinion it's dirty programming but I never found a good
argument to proof this.
Do you know examples where it's really important to catch Throwable (that
means catching Error, RuntimeException, ...)???

Regards
Michael
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, catching Throwable is not the right thing to do. A java.lang.Error usually indicates a very bad situation that means "all bets are off".

I like to let Error, and often RuntimeException, percolate up as far as possible in code. However, it may be worth catching either or both at the top of the call-stack, in main() of the application, or run() of the thread. This particularly if you want to do some logging or something.

Extra-special care should be taken of OutOfMemoryError, because this is a particularly bad Error. Squashing that is almost always going to be a bad idea.

If I see code that liberally sprinkles catch(Exception e) or catch(Throwable t), I tend to suspect that the programmer could not be bothered to think about error handling. In a quick-and-dirty utility, that can be excused; in industrial-strength code, it can't.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice answer!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic