简体   繁体   中英

Why this code is coming first in finally and then catch block?

package testing;

public class ExceptionHandling {
    public static void main(String[] args){
        try{
        int a=10;
        int b=0;
        int c=a/b;
        ExceptionHandling exp = null;
        System.out.println(exp);
        throw new NullPointerException();

        }catch(ArithmeticException e){
            System.out.println("arithmetic issue");
            throw new ArithmeticException();
        }
        catch(NullPointerException e){
            System.out.println("nullpointer");

        }
        finally{

            System.out.println("exception");
            //throw new ArithmeticException();
        }

    }

}

In console I am getting this:

arithmetic issue
exception
Exception in thread "main" java.lang.ArithmeticException
    at testing.ExceptionHandling.main(ExceptionHandling.java:15)

But why it gets printed finally block statement first and then catch block statement? It should print catch block statement first and then finally block statement.

This line in the console:

Exception in thread "main" java.lang.ArithmeticException
    at testing.ExceptionHandling.main(ExceptionHandling.java:15)

Is not being printed from your catch block. It's printed after your program leaves finally.

Here's how execution goes.

  1. Exception occurs in try .
  2. catch catches that exception.
  3. arithmetic issue is printed from catch block.
  4. next line re-throws that exception.
  5. your program is about to leave catch , but before it leaves, it executes finally block's code. That's why you see word exception in your console. That's how finally is designed to work.
  6. And at last you see actual exception on console when your program ultimately leaves this method.

it does not running first, the way println works is concurrent with the exception output. so they may print in various orders

This is how the flow goes:

  1. the catch statement catches the exception, prints the message but also throws the exception again
  2. the finally block is executed, so its message is printed
  3. the exception thrown in the catch is raised, since from the catch it's not been handled anyhow

Exceptions thrown by main() method will be handled by JVM and because you have recreated the ArithmeticException in your catch block and thrown it from the main method then the JVM has caught your ArithmeticException thrown by the main() method and printed the stacktrace on the console.

Your program flow is as follows:

(1) ArithmeticException thrown by the try block

(2) ArithmeticException has been caught by catch block and recreated a new ArithmeticException and thrown (by this main() method)

(3) finally block has been executed and printed the given text

(4) ArithmeticException thrown by this main() method has been caught by JVM

(5) JVM printed the stacktrace of the exception

To understand this concept better, just throw the exception from a different method and catch it from my main() as shown below:

    //myOwnMethod throws ArithmeticException
    public static void myOwnMethod() {
        try{
            int a=10;
            int b=0;
            int c=a/b;
            throw new NullPointerException();
        } catch(ArithmeticException e){
            System.out.println("arithmetic issue");
            throw new ArithmeticException();
        }
        catch(NullPointerException e){
            System.out.println("nullpointer");
        }
        finally{
            System.out.println("exception");
        }
    }

    //main() method catches ArithmeticException
    public static void main(String[] args) {
        try {
            myOwnMethod();
        } catch(ArithmeticException exe) {
            System.out.println(" catching exception from myOwnMethod()::");
            exe.printStackTrace();
        }
    }

But in your case, your main() has thrown the exception and the JVM has caught that Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM