posted 18 years ago
If an exception is not caught, the finally is executed, but all other code is skipped.
Here, in your example, the exception IS being caught. If an exception is caught, code flow returns to normal.
m1() throws an exception, but it catches it. So code flow returns to normal. Then m2() is run. It throws another exception, which is not caught in m2(). The finally-block runs anyway, as the finally block ALWAYS runs. The code jumps ahead to the next catch-block in main() where it is caught. After this, code flow returns to normal. Program exits without an error.
If there would be a method, say m3(), called after the call to m2(), this call would be skipped.
You can have nested try-catch-blocks. When an exception is thrown, the remainder of the try-block is skipped. If the exception is not caught, the finally-block (if there is one) ALWAYS runs. Once this try-catch-block is finished, one might still be in a higher-level try-catch-block. In this case, the remainder of the higher-level try-block is skipped. There is another chance for catch or finally (or both). There can be many levels of try-catch.
[ July 29, 2006: Message edited by: Douglas Chorpita ]