There are circumstances (such as logging, as you say) in which it could make sense to catch an
Error. However, it’s generally not something you can recover from; the application is in an unknown state, probably can’t continue, and the best approach is often to let the application shut down; it can then restart (automatically or manually).
I had one app which occasionally had
OutOfMemoryErrors (at least, until I fixed all the memory leaks…). The problem was that it was multithreaded; the error would crash a single thread (and free any object references held only by that thread), but the remaining threads would often continue merrily; depending which thread crashed, it might stop reading some input streams, or stop doing particular types of processing, or stop writing the results to a DB, or whatever – the effects could be quite subtle and easy for monitoring systems to miss. So the safest workaround was to catch all errors and shut down the whole app; it would then get restarted automatically, giving a short break in the data but avoiding any long-term effects.
There are some subtle difficulties in catching
Errors. In particular, since the app may be out of memory, anything you do in an error handler that constructs any new objects (even just
Strings) could fail with another
OutOfMemoryError. So if you’re doing any logging or other processing, at the least you need to trap any errors thrown by that and ensure that you shut down the app no matter what happens.