1

I stepped upon a case where I want to write my code like this:

try {  
    something();  
    return success;  
}  
catch (Exception e) {   
    return failure;  
}  
finally {  
    System.out.println("i don't know if this will get printed out.");
}

(from Does finally always execute in Java?)

My question is:

Is finally always executed before the method is finished or does it run in an extra thread (because the method is already finished with the object's return)?

In other words: Is it guaranteed that finally is executed before main method is continued?

Community
  • 1
  • 1
Arian
  • 3,183
  • 5
  • 30
  • 58
  • 3
    Imagine it like this: The compiler puts the code from the finally block before every return (and every other path of exiting from that method, like an exception). – Matthias Nov 18 '13 at 08:46

4 Answers4

4

Everything happens in the same thread, only the ordering is different from the program order:

  1. return x executes only in the part where x is determined as the return value;
  2. finally block executes;
  3. the value remembered in 1. is returned from the method—unless the finally block executes a return statement of its own/throws an exception.
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Short answer is, It's Part of method.

Even after the return the finally executes.

The finally block always executes when the try block exits.

Why ?

This ensures that the finally block is executed even if an unexpected exception occurs.

And to clarify your doubt. let's check the below code(For testing only, Not a good practice).

ry {  
    something();  
    return success;  
}  
catch (Exception e) {   
    return failure;  
}  
finally {  
    System.out.println("i don't know if this will get printed out.");
    return success;  //method returns this 
}

Now the method returns the value in finally

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

abhinav
  • 527
  • 3
  • 11
  • 24
  • 1
    And how do you kill the thread (without writing code inside the try/catch block which would result in the finally block being called) and keep the application running? – Matthias Nov 18 '13 at 08:55
  • Main thread may kill its children incase it goes into some error. – abhinav Nov 18 '13 at 11:08
  • There is no inherent parent-child relationship like this in Java threads. You would have to code this, and then again finally would apply if you set the block correctly. – Matthias Nov 18 '13 at 12:22
0

For try-finally or try-catch-finally: A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how control leaves the try block or catch block.

However, In terms of returning a value with a return statement:

  • The try and catch block's returning value with return statement is remembered.
  • If the finally block doesn't have a return statement, After executing the finally block:
    1. If no exception happened and(or) wasn't caught by the catch block: remembered returning value of the try block is returned.
    2. else, remembered returning value of the catch block is returned.
  • else, it will return value with the finally block's return statement forgetting about the try and(or) catch block's return.

     public static Integer returnData()
     {
        Integer x = null;
    
        try{
          System.out.println("try the x: "+x);
          return x;
        }
        finally
        {
           x = 5;  // here i am assigning 5
           System.out.println("finally is printed with: "+x);  // printing 5
           return x;
        }
    
     }
    
     public static void main(String[] args) {
        System.out.println("Printing the return value in main: "+returnData());
          // printed 5 according to the assignment in finally block
      }
    

Even if the above function should return null in the try block, you will see that running the above example will print:

try the x: null
Hi finally is printed with: 5
Printing the return value in main: 5

Now, if you remove the return statement from finally block, you will see the following output:

try the x: null
finally is printed with: 5
Printing the return value in main: null

As null was remembered as the returning value from the try block.

check out the jls: 14.20.2. Execution of try-finally and try-catch-finally For more details.

Sage
  • 15,290
  • 3
  • 33
  • 38