2

I have a class- public class Check {

public static void main(String[] args) {
    System.out.println(new Check().isRight());
}


public boolean isRight(){
    try{
        return true;
    }finally{
        return false;
    }
}

The method isRight has return true and false in finally clause , and finally it returns me false as finally clause will surely be called, so whats actually happening the data inside try. I need to know the logical reason behind the output , I read but couldn;t understand properly.

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
  • 3
    Why would you write such a messy code? – m0skit0 Dec 07 '13 at 14:54
  • possible duplicate of [Strange finally behaviour?](http://stackoverflow.com/questions/11187148/strange-finally-behaviour) or [Java-try-finally-return-design-question](http://stackoverflow.com/questions/4185340/java-try-finally-return-design-question) – Pshemo Dec 07 '13 at 14:54
  • i just want this info for learning . nothing about exact code – Abhishek Choudhary Dec 07 '13 at 14:54
  • http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java – NPE Dec 07 '13 at 14:55
  • 2
    @bunta Compiler throws a warning about the finally block not terminating properly, so even the compiler thinks this code is messsy. – m0skit0 Dec 07 '13 at 14:56
  • quite an odd question, all basic logic would tell you the answer is that finally is called..finally..so it returns..false.. – Dylan Meeus Dec 07 '13 at 14:57
  • OK, I don't know why this question still isn't closed. It is obviously duplicate... – ferrerverck Dec 07 '13 at 15:04
  • At call-stack level, the value of the frame that contains the return value is first populated by the code in the try block. But eventually as control goes to finally, the code in finally block overwrites the return frame. Hence, the return data produced by code inside the try block is overwritten by the code inside finally block. For better understanding read: http://en.wikipedia.org/wiki/Call_stack – Amit Sharma Dec 07 '13 at 15:19

3 Answers3

5

finally will not be called only if you use System.exit() or JVM craches.

In all other cases, finally will be called, so false will be returned. How?

When the return in try is reached, the control transfers to finally, there you return false.. Simple.

Go through the official docs to better understand that.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

Your code always returns false because the finally block is always executed after try-catch statements are executed no matter what unless the JVM crashes or on System.exit() as others have already mentioned.

So, when the try block reaches the return true; statement the control is passed however to the finally block and it returns false (over-riding the previous true value).

rooni
  • 1,036
  • 3
  • 17
  • 33
lvarayut
  • 13,963
  • 17
  • 63
  • 87
0

false will be returned there. That's the idea of finally - it gets the last say on things. finally will be invoked and will return false as mentioned in your code.

Jops
  • 22,535
  • 13
  • 46
  • 63