0

I have a method with a try and catch block, and the code after the finally statement did not print ("end of a" line).

The method threw an exception and I suspect this is the reason. Is the reason for the line

System.out.println("end of a"); 

not printing because of the exception?

Here's the code:

Test Class:

    public class Test
{
    Integer num;
    public Test(){
        this(0) ;
        System.out.println("Test constructor1 ");
    }
    public Test(int i){
        if (i>0) num = new Integer(i) ;
        System.out.println("Test constructor2 ");
    }
    public void a() throws Exception{
        try{
            if (num.intValue() > 0)
                System.out.println("num = "+num.intValue());
            System.out.println("executing a ");
            throw new Exception();
        }catch (Exception e){
            System.out.println("exception caught in a");
            if (e instanceof NullPointerException) throw e;
        }
        finally{
            System.out.println("finally in a");
        }
        System.out.println("end of a");
    }
}

Main Class:

public class MainTest2{
    public static void main(String args[]){
        try{
            Test t2 = new Test();
            t2.a();
            System.out.println("executing main1 ");
        }catch (Exception e){
            System.out.println("exception caught in main");
        }
        System.out.println("ending main ");
    }
}
Assaf
  • 1,112
  • 4
  • 14
  • 35
  • 2
    Is the reason for the line `System.out.println("end of a");` not printing because of the exception? **Exactly** – Olaf Kock Apr 02 '18 at 15:51
  • @OlafKock Ok, thanks. So I can understand that if an exception is thrown in a method, **only** the finally block will be printed and not anything else? – Assaf Apr 02 '18 at 15:52
  • 1
    if *you* don't `catch` an exception, nothing else does. `finally` just briefly interrupts the thrown exception before it's thrown further – Olaf Kock Apr 02 '18 at 15:54

2 Answers2

1

Step by Step:

  1. when you are calling a() in t2 the value of num in t2 is null or not set in other words
  2. if you run the if (num.intValue() > 0) it creates a NullPointerException (reason see step 1)
  3. so the occured exeption triggers the try and it jumps into the catch block and gives a NPE via the e over to the catch block
  4. the catch block tests for a NPE in e, this is true and so the throw e passes the exeption to the next instance
  5. the finally block in a() is beeing executed
  6. the programm leaves the try-catch-finally block, while having an unhandeled exeption open from Step 4
  7. Step 6 triggers the requirement for the throws Exception in the declaration of a() and therefor a() stops executing and returns the exeption to main()
  8. now main() is responsible for the exeption and running

Conclusion:
The line System.out.println("end of a"); is never reached by the programm because it ran into a unhandeled exeption earlier, throw the exeption and stoped executing a() befor the line.

(Sorry for spelling or grammer errors :))

ProfBits
  • 199
  • 1
  • 9
0

The main reason for the exception is NullPointerException caused due to num.intValue().
So when the exception arises System.out.println("finally in a"); gets executed.
After this, since e is actually an instance of NPE the code returns directly on executing his piece code if (e instanceof NullPointerException) throw e;
And rhe last sysout is never executed.

swayamraina
  • 2,958
  • 26
  • 28