Finally block excecution

173 Views Asked by At

When the below method is called it's printing output as 6 but I am expecting output as 5 as I have re-assigned n = 5 in the finally block.

Can anybody please help me with this?

public static int p() {
    int n = 0;
    try {
        n = 6 ;
        return n;
    } catch (Exception e) {
        return n;
    } finally {
        n = 5;
    }
}
3

There are 3 best solutions below

3
Nagaraju Chitimilla On

As mentioned by luk2302 you are already return the value as 6. If you want to return the value as 5 then change your method as shown below.

public static int p() {
    int n = 0;
    try {
      n = 6;
    } catch (Exception e) {
      return n;
    } finally {
      n = 5;
    }
    return n;
  }
2
i v s narayana On

Question is very interesting, i even surprised why it is happening like that. When i checked about finally block i got the definition like below

"Java finally block is a block used to execute important code such as closing the connection, etc. Java finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not."

But i have compiled your program and saw how the compiled class looks like. It gives the answer

Original Java class

public class Finally {

    public static int p() {
        int n = 0;
        try {
            n = 6 ;
            return n;
        } catch (Exception e) {
            return n;
        } finally {
            n = 5;
        }
    }

    public static void main(String[] args) {

        int n = p();
        System.out.println("value of n " + n);

    }
}

Compiled class

public class Finally {
    public Finally() {
    }

    public static int p() {
        byte n = 0;

        byte var2;
        try {
            n = 6;
            byte var1 = n;
            return var1;
        } catch (Exception var6) {
            var2 = n;
        } finally {
            boolean var8 = true;
        }

        return var2;
    }

    public static void main(String[] args) {
        int n = p();
        System.out.println("value of n " + n);
    }
}

As you can see how return statements translated due to that we don't see the value which is assigned in finally block instead we see value which is assigned in the try block.

Hope that clarify your answer.

0
Popeye On

This can easily be answered by understanding the order of execution of your code. In your scenario you will always be returning the value of n before you hit the final block, in your code you will always be returning the value of 6. You will never return 5 (Final Block) or 0 (Catch Block).

So why would you never get 0?

You would never get 0 because the code within the try part of your try-catch-final statement will never in a million years throw any exception the way it has been written, so the catch statement is redundant.

So why would you never get 5?

You would never get 5 because the order of execution is return statement in the try block is executed first and then the final block runs. A try-final statement is the only statement I can think of (Happy to be proven wrong in comments) that any code is executed after a return statement is executed.

There is no reason why in your scenario you would have that final block unless for whatever reason you didn't trust the Garbage Collector was doing it's job, in which case you would nullify the n property here and that's it.

Your code could easily be re-written as the below because 6 is the only value your code will ever return.

public static int p() {
    return 6;
}