Why should I not wrap every block in "try"-"catch"?

Konrad picture Konrad · Apr 29, 2010 · Viewed 66.3k times · Source

I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block.

I just posted 'You should ALWAYS wrap calls that can throw in try, catch blocks.' to this question and was told that it was 'remarkably bad advice' - I'd like to understand why.

Answer

Mitch Wheat picture Mitch Wheat · Apr 29, 2010

A method should only catch an exception when it can handle it in some sensible way.

Otherwise, pass it on up, in the hope that a method higher up the call stack can make sense of it.

As others have noted, it is good practice to have an unhandled exception handler (with logging) at the highest level of the call stack to ensure that any fatal errors are logged.