Exception Handling output messages
<p>I have wrote the code below which was taken from Java How to program 9th Edition - Paul and Michelle Harvey - The code works fine but the problem is that every time I execute it, it gives me uncertain results in which the exceptions are handled - e.g. please look at the output of the code snippet. can you please help me understand why this behavior is occurring ?</p>

<pre><code> public class Test {

    public static void main(String[] args) {

        try {
            // call method throwException
            throwException();
        }// end try

        catch (Exception e) {
            System.out.println("Exception handled in main");
        }// end catch

        // call method doesNotThrowException
        doesNotThrowException();

    }

    private static void throwException() throws Exception {
        try {
            System.out.println("Method throwException.");
            throw new Exception(); // generate exception
        }

        catch (Exception exception) {
            System.err.println("Exception handled in method throwException");
            throw exception;
        }

        // executes regardless of what occurs in try ... catch block
        finally {
            System.err.println("Finally executed in throwException.");
        }
    }// end of method throwException

    private static void doesNotThrowException() {
        try {
            System.out.println("Method doesNotThrowException.");
        }

        // catch does not execute as the method does not throw any exceptions
        catch (Exception exception) {
            System.err.println(exception);
        }// end catch

        // executes regardless of what occurs in try ... catch block
        finally {
            System.err.println("Finally executed in doesNotThrowException");
        }
    }// end of deosNotThrowException

}//end Test Class
</code></pre>

<p>OUTPUTS:
1)</p>

<pre><code>Method throwException.
Exception handled in method throwException
Finally executed in throwException.
Finally executed in doesNotThrowException
Exception handled in main
Method doesNotThrowException.
</code></pre>

<p>2)</p>

<pre><code>Exception handled in method throwException
Finally executed in throwException.Method throwException.

Finally executed in doesNotThrowException
Exception handled in main
Method doesNotThrowException.
</code></pre>
