Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
C. Ross Avatar asked Sep 03 '09 18:09

C. Ross


People also ask

What happens if an exception is not caught in Java?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

What happens if the main () method throws an exception that is not caught?

If a non-checked exception is thrown (and not catch) in the main method, it will also terminate. Well, test it, if you throw a checked exception other than the FileNotFoundException it won't compile. Otherwise, if the method throws a unchecked exception, then It will simple end propagating the unchecked exception.

Which exceptions Cannot be caught?

The only exception that cannot be caught directly is (a framework thrown) StackOverflowException. This makes sense, logically, as you don't have the space in the stack to handle the exception at that point.

Can we throw exception without catch?

You can avoid catching an exception, but if there is an exception thrown and you don't catch it your program will cease execution (crash). There is no way to ignore an exception. If your app doesn't need to do anything in response to a given exception, then you would simply catch it, and then do nothing.


1 Answers

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

like image 147
Jon Skeet Avatar answered Oct 14 '22 05:10

Jon Skeet