views:

761

answers:

6

Why are some methods 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 thoguht catching exceptions would catch all exceptions? How can I catch all exceptions in java?

+13  A: 

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.

Any exception deriving from Exception (or Exception itself) 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...

Jon Skeet
+1: nice diagram!
akf
Errors aren't really exceptions, hence why they don't derive from Exception.
R. Bemrose
@R. Bemrose - they are according to the Java Language Specification: "Every exception is represented by an instance of the class Throwable or one of its subclasses" and "The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses."
Jon Skeet
+3  A: 

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.

akf
+3  A: 

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

Carl Manaster
A: 

As both other posts point out, catch(Exception e) will only work for exceptions that derive from Exception. However, if you look at the tree hierarchy, you'll notice that an Exception if Throwable. Throwable also is the base class for Error as well. So, in the case of NoSuchMethodError, it is an Error and not an Exception. Notice the naming convention *Error vs. *Exception (as in IOException, for example).

JasCav
+1  A: 

As other posters have pointed out, not all throwable objects are subclasses of Exception. However, in most circumstances, it is not a good idea to catch Error or Throwable, because these conditions include some really serious error conditions that cannot easily be recovered from. Your recovery code may just make things worse.

Simon Nickerson
Will finally still execute around an error?
C. Ross
Yes. But you may have further problems. e.g. what happens if/when you catch an OutOfMemoryError ?
Brian Agnew
+4  A: 

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.

R. Bemrose
Java language specification disagrees with you: "The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses."
Jon Skeet
I especially appreciate the advice on not catching Errors. Consider `OutOfMemoryErrors` - an application may be left in a bad state once this is thrown. It would not be wise to catch and discard this sort of `Error`.
akf
Basically, the JLS always refers to "things that can be thrown and caught" as exceptions. Nothing you've quoted there says that an Error isn't an exception.
Jon Skeet