Java exception not caught? Java exception not caught? java java

Java exception not caught?


From the Java Language Specification 14.20.2.:

If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:

  • If the finally block completes normally, then the try statement completes abruptly for reason R.

  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

So, when there is a catch block that throws an exception:

try {    // ...} catch (Exception e) {    throw new Exception("2");}

but there is also a finally block that also throws an exception:

} finally {    throw new Exception("3");}

Exception("2") will be discarded and only Exception("3") will be propagated.


Exceptions thrown in finally block suppress the exception thrown earlier in try or catch block.

Java 7 example: http://ideone.com/0YdeZo

From Javadoc's example:


static String readFirstLineFromFileWithFinallyBlock(String path)                                                     throws IOException {    BufferedReader br = new BufferedReader(new FileReader(path));    try {        return br.readLine();    } finally {        if (br != null) br.close();    }}

However, in this example, if the methods readLine and close both throw exceptions, then the method readFirstLineFromFileWithFinallyBlock throws the exception thrown from the finally block; the exception thrown from the try block is suppressed.


The new try-with syntax of Java 7 adds another step of exception suppression: Exceptions thrown in try block suppress those thrown earlier in try-with part.

from same example:

try (        java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);        java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)    ) {        for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {            String newLine = System.getProperty("line.separator");            String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;            writer.write(zipEntryName, 0, zipEntryName.length());        }    }

An exception can be thrown from the block of code associated with the try-with-resources statement. In the above example, an exception can be thrown from the try block, and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the ZipFile and BufferedWriter objects. If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the writeToFileZipFileContents method. You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.


In code from question, each block is plainly discarding the old exception, not even logging it, not good when you are trying to resolve some bugs:

http://en.wikipedia.org/wiki/Error_hiding


Since throw new Exception("2"); is thrown from catch block and not try, it won't be caught again.
See 14.20.2. Execution of try-finally and try-catch-finally.

This is what happening:

try {    try {        System.out.print("A");         //Prints A        throw new Exception("1");       } catch (Exception e) {         System.out.print("B");         //Caught from inner try, prints B        throw new Exception("2");       } finally {        System.out.print("C");         //Prints C (finally is always executed)        throw new Exception("3");      }} catch (Exception e) {    System.out.print(e.getMessage());  //Prints 3 since see (very detailed) link}