:icon_lol:Suppose there is a (method) call stack; An exception has occured in a deep level but not handled there yet; So the corresponding finally block at that level is executed first and then it is thrown to be propagated up through the call stack and at each step if it finds it executes the finally block first and then it is thrown again to be propagated up through the call stack unless it is caught. Now this exception is caught at some (middle) level of the call stack; Here also the finally block of only that corresponding level is then executed and then the program terminates.
:icon_rolleyes:Are these possible/true? What about the other finally blocks which were in the upward side of the call stack those were out of the reach of the propagation up, after handling the exception? Will they ever be executed!? Please clear the concept .......

My first question is... What would happen if you call the System.exit() in the finally clause from the middle of the exception stack level? Would that terminate the program without going up to the top level?

try {
  ...
}
catch (Exception e) {
  ...
}
finally {
  System.exit(1);
}

My second question is... What would happen if there exists an uncaught exception during your mid-level stack of try-catch and then the finally of that level is executed?

try {
  ...
}
catch (FileNotFoundException e) {  // not all exceptions are caught
  ...
}
finally {
  ...
}

Something to think about in order to answer your scenario... :)

Edited 5 Years Ago by Taywin: n/a

A trawl through the JLS and posts on the more credible Java sites gives a consistent answer:
If the code has entered one or more try blocks then every associated finally block will be executed unless the JVM is terminated, eg by calling System.exit . (As one wag wrote "that's why it's called finally , not probably )
It's not hard to construct some simple test cases to demonstrate that this is true.

Edited 5 Years Ago by JamesCherrill: n/a

Then should it be meant that the rest of the finally blocks those were above the reach of the propagation up after catching the exception in the call stack, can never be executed?
If so, how the method

super.finalize()

may be guaranteed to be executed in a finally block explicitely where the implicit garbage collection facility may not be availed [[/B]if the finally block becomes out of reach at all[B]] before terminating the program?
Please also specify the sites .......

This article has been dead for over six months. Start a new discussion instead.