You should only catch exceptions that you know how to process properly; examples include IOException
where you can do something like retry an operation, return some default value, or rethrow the error; and NumberFormatException
, where you're trying to read user input as a number and find that it's garbage, so you can ask the user to try again.
In nearly all cases, you don't actually know what the proper response to "any error" is, and in many cases (most unchecked exceptions, for example), the only thing you'll be doing by catching Exception
is masking some underlying problem that needs to be resolved. In general, the only response that is acceptable for a generic unknown exception is "write a log message and abort the current operation" for whatever definition of "current operation" applies (might include things like rolling back a transaction and returning an HTTP 503 status code).
In real-world applications, this last-resort catch Exception
is handled by framework code (such as Spring or Jersey) and does these broad cleanup operations. If your code can't do anything better (which generally requires knowing what specifically happened, not just "an exception"), then it should let the exception propagate and use the standard error handlers.