Java exception not caught?
Because some exceptions don't derive from Exception
- e.g. Throwable
and Error
.
Basically the type hierarchy is:
Object
|
Throwable
/ \
Exception Error
Only Throwables
and derived classes can be thrown, so if you catch Throwable
, that really will catch everything.
Throwable
, Exception
and any exception deriving from Exception
other than those derived from RuntimeException
count as checked exceptions - they're the ones that you have to declare you'll throw, or catch if you call something that throws them.
All told, the Java exception hierarchy is a bit of a mess...
Exception is just one kind of Throwable; NoSuchMethodError is not an Exception, but an Error, which is another kind of Throwable.
Error
s aren't Exception
s.
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
-- JavaDoc for java.lang.Exception
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
-- JavaDoc for java.lang.Error
There are certain errors that you may want to catch, such as ThreadDeath. ThreadDeath is classified as an Error, as explained below
The class ThreadDeath is specifically a subclass of Error rather than Exception, even though it is a "normal occurrence", because many applications catch all occurrences of Exception and then discard the exception.
-- JavaDoc for ThreadDeath
However, since Thread's stop() method is now deprecated, you should not use it, and thus you should never see ThreadDeath.