Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try-catch-finally with return after it

Tags:

I know how try, catch & finally work (for most part), but I have one thing I was wondering: what happens with a return statement after a try-catch-finally, while we already had a return in the try (or catch)?

For example:

public boolean someMethod(){     boolean finished = false;     try{         // do something         return true;     }     catch(someException e){         // do something     }     finally{         // do something     }     return finished; } 

Let's say nothing went wrong in the try, so we returned true. Then we will go to the finally where we do something like closing a connection, and then?

Will the method stop after we did some stuff in the finally (so the method returned true in the try), or will the method continue after the finally again, resulting in returning finished (which is false)?

Thanks in advance for the responses.

like image 943
Kevin Cruijssen Avatar asked Mar 20 '14 12:03

Kevin Cruijssen


People also ask

Will finally be executed after return in catch?

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.

Can we have a return statement in try catch and finally block?

Yes, we can write a return statement of the method in catch and finally block.

What should I return after try catch?

All code after it will be perfectly reachable. And finally returns nothing at all. It is just a block of code that is executed unaware of if there was an exception or not.

What happens if catch and finally both return value?

When catch and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by catch block.


2 Answers

The fact that the finally block is executed does not make the program forget you returned. If everything goes well, the code after the finally block won't be executed.

Here is an example that makes it clear:

public class Main {      public static void main(String[] args) {         System.out.println("Normal: " + testNormal());         System.out.println("Exception: " + testException());     }      public static int testNormal() {         try {             // no exception             return 0;         } catch (Exception e) {             System.out.println("[normal] Exception caught");         } finally {             System.out.println("[normal] Finally");         }         System.out.println("[normal] Rest of code");         return -1;     }      public static int testException() {         try {             throw new Exception();         } catch (Exception e) {             System.out.println("[except] Exception caught");         } finally {             System.out.println("[except] Finally");         }         System.out.println("[except] Rest of code");         return -1;     }  } 

Output:

[normal] Finally Normal: 0 [except] Exception caught [except] Finally [except] Rest of code Exception: -1 
like image 108
Joffrey Avatar answered Oct 23 '22 00:10

Joffrey


If all goes well, return inside the try is executed after executing the finally block.

If something goes wrong inside try, exception is caught and executed and then finally block is executed and then the return after that is executed.

like image 43
Dhanush Gopinath Avatar answered Oct 23 '22 00:10

Dhanush Gopinath