File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes catch Throwable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "catch Throwable" Watch "catch Throwable" New topic

catch Throwable

Michael Imhof

Joined: Nov 07, 2004
Posts: 24
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, ...)???

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Nice answer!

[Jess in Action][AskingGoodQuestions]
jQuery in Action, 3rd edition
subject: catch Throwable
jQuery in Action, 3rd edition