Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
0

In Java, does the finally block gets executed if we insert a return statement inside the try block of a try-catch-finally?

Vinay Goli
08/28/2011 at 15:25
6 Answers
1
1

Yes. Finally is always executed as long as the object is still alive.

Kokok Tolentino
08/28/2011 at 23:17

2
0

I must say that the try-catch-finally block can be executed within a static method, so there's no object to be alive.

So I think the answer should just be "Yes, finally block is always executed".

Gerardo Robles
08/29/2011 at 10:39

3
0

as per java finally block has to be executed....but logically.. we have return in try block..!! so wat does JVM do??

Vinay Goli
08/29/2011 at 12:10

4
0

'finally' is always executed even if it has return statement in try block.

Trupti Balsaraf
09/03/2011 at 08:24

5
0

finally is NOT executed if a return statement is executed before it. Finally is associated with try catch blocks and not with program (or method) flow in general.

Ali Ahsan
09/06/2011 at 19:46

6
0

finally block is always executed even though return statement present in try block.

here is a simple sample code

 

public class FinallyTest {
    public static void main(String[] args) {
        call();
    }
   
    public static int call() {
        try {
            System.out.println("Inside try block");
            return 1;
        } catch (Exception e) {
        } finally {
            System.out.println("Inside finally block");
        }
        System.out.println("At the end of the method");
        return 1;
    }
}

Prasad Khode
09/06/2011 at 22:29

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details