When to catch java.lang.Error?

João picture João · Dec 9, 2008 · Viewed 151.5k times · Source

In what situations should one catch java.lang.Error on an application?

Answer

Yoni Roit picture Yoni Roit · Dec 9, 2008

Generally, never.

However, sometimes you need to catch specific errors.

If you're writing framework-ish code (loading 3rd party classes), it might be wise to catch LinkageError (no class def found, unsatisfied link, incompatible class change).

I've also seen some stupid 3rd-party code throwing subclasses of Error, so you'll have to handle those as well.

By the way, I'm not sure it isn't possible to recover from OutOfMemoryError.