The o/p will be 0,3,4. Reason--- if(i/j>1) throws ArithmeticException that is caught by catch(ArithmeticException ae) -----> prints 0 finally block gets executed -----> prints 3 control continues after finally ----> prints 4 The points that gets clear from ur code is 1-> finally block gets executed in every case i.e. Exception or no exception . 2-> after finally / catch block the program continues through the next block of statements.
Deeksha, I think you are a little bit wrong. After finally gets executed, the flow goes back to the calling method which calls the present method( not the next block of statements). I agree that your o/p is correct.
Hi danny, if u agree with my answers then u should agree that after executing finally block the control moves to next block of statement (if present).otherwise how would 4 get printed. in case there is nothing written in the function after the finally block then certainly the control will get transferred to the caller function. now do u agree with me ........ deekasha
Hi Charu, The finally block will get executed wether or not the exception is thrown or handled. The only time the finally will not get executed is when you invoke the System.exit(); On all other times finally will be called. Pichai
Hi, try block must be at least followed by either a catch block or a finally block.or both may be there. so in case there is no catch after try block but still finally block is there then it's okay with the compiler. in case there are both catch and finally blocks then finally block shall be the last. regards deekasha
I'm not sure if it was clearly stated above. However, the last line of code was only executed because the exception was caught`. Had the exception not been handled, the finally block would have been executed, execution of the mothod would have ceased, and the exception would have been thrown to the calling method (assuming this method could throw this exception).