Java exception not caught?
Java exception not caught?
I have a small theoretical problem with try-catch constructions.
I took a practical exam yesterday about Java and I don't understand following example:
try { try { System.out.print("A"); throw new Exception("1"); } catch (Exception e) { System.out.print("B"); throw new Exception("2"); } finally { System.out.print("C"); throw new Exception("3"); } } catch (Exception e) { System.out.print(e.getMessage()); }
The question was "what the output will look like?"
I was pretty sure it would be AB2C3, BUT suprise suprise, it's not true.
The right answer is ABC3 (tested and really it's like that).
My question is, where did the Exception("2") go?
Answer by S.D. for Java exception not caught?
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
Answer by Adam Siemion for 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.
Answer by Maroun Maroun for Java exception not caught?
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 }
Answer by allprog for Java exception not caught?
The finally
block always runs. Either you return
from inside the try block or an exception is thrown. The exception thrown in the finally
block will override the one thrown in the catch branch.
Additionally, throwing an exception will not cause any output by itself. The line throw new Exception("2");
will not write anything out.
Answer by Sarkar for Java exception not caught?
Your Question is very obvious, and the answer is simple to the same extent.. The Exception object with message as "2" is overwritten by the Exception object with message as "3" .
Explanation : When an Exception occur, its object it thrown to catch block to handle. But when exception occur in catch block itself, its object is transferred to OUTER CATCH Block(if any) for exception Handling. And Same happened Here. The Exception Object with message "2" is transferred to OUTER catch Block . But wait.. Before leaving inner try-catch block it HAS TO EXECUTE FINALLY. Here occurred the change we are concerned about. A new EXCEPTION object(with message "3") is thrown out or this finally block which replaced the already thrown Exception object(with message "2").As a result of which, when the message of Exception object is printed , we got overridden value i.e. "3" and not "2".
Keep Remember :Only one exception object can be handled by on CATCH block.
Answer by nazar_art for Java exception not caught?
According to your code:
try { try { System.out.print("A"); throw new Exception("1"); // 1 } catch (Exception e) { System.out.print("B"); // 2 throw new Exception("2"); } finally { // 3 System.out.print("C"); // 4 throw new Exception("3"); } } catch (Exception e) { // 5 System.out.print(e.getMessage()); }
As you can see here:
- print A and throws exception
# 1
; - this exception has caught by catch statement and print
B - # 2
; - block finally
# 3
executes after try-catch (or only try, if hadn't occurred any exception) statement and printsC - # 4
and thrown new exception; - this one has caught by external catch statement
# 5
;
Result is ABC3
. And 2
is omitted at the same way as 1
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment