Java exception not caught
JavaExceptionTry CatchJava Problem Overview
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?
Java Solutions
Solution 1 - 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...
Solution 2 - Java
> 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.
However, since Thread's stop() method is now deprecated, you should not use it, and thus you should never see ThreadDeath.
Solution 3 - Java
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.
Solution 4 - Java
Exception is just one kind of Throwable; NoSuchMethodError is not an Exception, but an Error, which is another kind of Throwable.
Solution 5 - Java
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.
Solution 6 - Java
First let's clear up some unfortunate semantic confusion in this discussion. There is the java.lang.Exception
class which we can refer to simply as Exception with a capital 'E'. Then you have exception with a lowercase 'e' which is a language feature. You can see the lower case version in the documentation for the Throwable
class:
> For the purposes of compile-time checking of exceptions, Throwable and > any subclass of Throwable that is not also a subclass of either > RuntimeException or Error are regarded as checked exceptions.
For me it's easier to think about the answer to this question as checked vs. unchecked exceptions (lower case e). Checked exceptions must be considered at compile time, whereas unchecked exceptions are not. Exception (uppercase E) and it's subclasses are checked exceptions, which means you either have to catch any exception that can be thrown by your code, or declare the exceptions that your method could throw (if not caught).
Error and it's subclasses are unchecked exceptions, which means your code neither has to catch an Errors that could be thrown, nor do you have to declare that you throw those Errors. RunTimeException and its subclasses are also unchecked exceptions, despite their location in the class hierarchy.
Consider the following code:
void test() {
int a = 1, b = 0, c = a / b;
}
When run, the above code will produce a java.lang.ArithmeticException
. This will compile without any errors, even though an exception is thrown and the code neither catches the ArithmeticException nor declares that it throws this exception. This is the essence of unchecked exceptions.
Consider the location of ArithmeticException
in the class hierarchy, especially the fact that this is a subclass of java.lang.Exception. Here you have an exception that derives from java.lang.Exception but because it's also a subclass of java.lang.RuntimeException, it's an unchecked exception so you don't have to catch it.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.ArithmeticException
If you want to catch anything that could possibly be thrown, catch a Throwable. However this may not be the safest thing to do because some of those Throwables could be fatal runtime conditions that perhaps should not be caught. Or if you do catch Throwable, you may want to re-throw Throwables that you can't deal with. It depends on the context.
Solution 7 - Java
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).