1
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.

arnab layek
  • 157
  • 1
  • 11
  • 1
    Your output prints `catch` block first and then `finally`. Why do you think it's printing otherwise. – Codebender Mar 26 '17 at 08:08
  • 1
    Check this answer. [link](http://stackoverflow.com/questions/3109353/what-comes-first-finally-or-catch-block). Also, you don't need to throw exception in try block itself. – budthapa Mar 26 '17 at 08:16

4 Answers4

2

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.
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
1

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

TheMMSH
  • 501
  • 6
  • 26
  • That output is consistent with order of execution. There is no "various orders" observed in the question. – Andreas Mar 26 '17 at 08:13
  • @Andreas "That output is consistent with order of execution" this is not true, i've run the exact code above and had [this](http://imgur.com/DdDYNyB) ("exception" printed in middle of the error handling) but in this case my answer is not true neither. i mean the order of execution is really the way he said. – TheMMSH Mar 26 '17 at 08:52
1

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
Stefano Zanini
  • 5,876
  • 2
  • 13
  • 33
0

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.

Vasu
  • 21,832
  • 11
  • 51
  • 67