0

My code is below :-

class Varr {    
    public static void main(String[] args) {    
        try {
            System.out.println(10/0);
        }
        catch(ArithmeticException e) {
            System.out.println("catch1");
            System.out.println("catch1");
            throw new ArithmeticException ("Exce");             
        }
        finally {   
            System.out.println("finally");
        }
    }
}

Output is :-

catch1

catch1

finally

Exception in thread "main" java.lang.ArithmeticException: Exce
  at one.Varr.main(Varr.java:22)

As per my knowledge the flow has to be first try then catch and finally at last but as per the output the flow is try then few lines of catch upto the throw exception statement and then finally and the throw exception statement of catch block at last.

Why is there discrepancy in flow, I mean why finally was executed before the throw new exception statement of catch block

2 Answers2

0

Because a block of finally, by definition, has to be executed no matter the outcome of the try or catch clauses.

In your case, the run-time knows there is an exception that has to be propagated upwards, but before doing so, it executes whatever is inside the finally block.

When the finally block is finished, it propagates any exception that might have been raised, or the flow continues otherwise.

alirabiee
  • 1,286
  • 7
  • 14
  • Thanks for the answer, just a clarification please :- In case at run time if throw new exception statement of catch block is executed before the finally block then the exception object will get handover to the JVM and the programs terminates abnormally without finally getting chance, right ? and also if there is an exception in finally block itself then exception will be thrown of finally block without executing the throw exception statement of catch block, right ? – springcloudlearner Sep 10 '17 at 17:00
  • What do you need to have clarified? – alirabiee Sep 10 '17 at 17:01
  • The order is like this: try-catch(es)-finally. After finally, if there's any exception on-hold, it will be thrown, otherwise it continues. – alirabiee Sep 10 '17 at 17:03
  • Thanks for the answer, just a clarification please :- In case at run time if throw new exception statement of catch block is executed before the finally block then the exception object will get handover to the JVM and the programs terminates abnormally without finally getting chance, right ? and also if there is an exception in finally block itself then exception will be thrown of finally block without executing the throw exception statement of catch block, right ? – springcloudlearner Sep 10 '17 at 17:07
  • Take it as a buffered throw. Like if there is a finally block, any un-caught or re-raised exception before the start of the finally block is put into the buffer, and when it finishes, it is propagated upwards if the buffer is not empty. As you said if there's any exception in the finally block it is immediately thrown. – alirabiee Sep 10 '17 at 17:12
  • Thanks for the answer :) – springcloudlearner Sep 10 '17 at 17:13
0

You can take a look at the essentials of finally

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • if this is the case then why first "System.out.println("catch1"); System.out.println("catch1");" statements of catch block gets executed before the finally block ? – springcloudlearner Sep 10 '17 at 17:07
  • @bharatbhushan *This ensures that the finally block is executed even if an unexpected exception occurs.*...so once the unexpected exception occurs the finally is executed and then the exception is thrown. Point to note is until your second `System.out.println("catch1");` there is nothing like an unexpected exception. – Naman Sep 10 '17 at 17:10
  • Thanks for the answer, just a clarification please :- In case at run time if throw new exception statement of catch block is executed before the finally block then the exception object will get handover to the JVM and the programs terminates abnormally without finally getting chance, right ? and also if there is an exception in finally block itself then exception will be thrown of finally block without executing the throw exception statement of catch block, right ? – springcloudlearner Sep 10 '17 at 17:11
  • *programs terminates abnormally* => from the same doc -- *If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.* – Naman Sep 10 '17 at 17:12