Finally block in Java

Discussion in 'Programmer's Corner' started by arman19940326, Aug 5, 2016.

  1. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    Scrapbook_1470393724828.png
    This program tries to divide two numbers by a method. The method is a bit strange. First when it tries to divide the two numbers for this example it throws an exception. The catch block catches that exception and outputs a message to the console and then tries again to divide the numbers. So another exception is thrown in the catch block. My question is when this method throws this exception to the caller method? After the execution of the finally block or before? Or maybe after the execution of the entire method? It seems the moment when an exception is thrown in the catch block all other lines are ignored and the compiler jumps to the finally block, executes its commands and then the method throws the ArithmeticException to the caller method (here it's main method) . As a result the lines 35 and 36 are not executed in this example. Is my conclusion a general rule when we have a finally block and an exception in a catch block?
     
Loading...