Java exception not caught

Why are some exceptions in Java not caught by catch (Exception ex)? This is code is completely failing out with an unhandled exception. (Java Version 1.4).

public static void main(String[] args) {
    try {
        //Code ...
    } catch (Exception ex) {
        System.err.println("Caught Exception");
        ex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    }
    finally {
        app.shutdown();
    }
    System.exit(exitCode);
}

I get a Exception in thread "main" java.lang.NoSuchMethodError

But this works

public static void main(String[] args) {
    int exitCode = app.SUCCESS_EXIT_CODE;
    try {
        //Code ...
    } catch (java.lang.NoSuchMethodError mex){
        System.err.println("Caught NoSuchMethodError");
        mex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    } catch (Exception ex) {
        System.err.println("Caught Exception");
        ex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    }
    finally {
        app.shutdown();
    }
    System.exit(exitCode);
}

I get Caught NoSuchMethodError java.lang.NoSuchMethodError:

I thought catching exceptions would catch all exceptions? How can I catch all exceptions in java?


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...


Errors aren't Exceptions.

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.


Exception is just one kind of Throwable; NoSuchMethodError is not an Exception, but an Error, which is another kind of Throwable.


You can catch Throwable. Error and Exception extend Throwable.

see the Throwable JavaDoc:

The Throwable class is the superclass of all errors and exceptions in the Java language.