In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ?
-
7When you tried it, what happened? – Mark Peters Aug 22 '11 at 06:21
-
3The `finally` block will always get executed. That's why it's called finally :-) – home Aug 22 '11 at 06:22
-
5Whatever you do, don't `return` from a finally block. – Thilo Aug 22 '11 at 06:23
-
2@Thilo, +1. Once (many-many years ago) I wrote return from finally and then spent half a day looking for the problem it caused... – AlexR Aug 22 '11 at 06:26
-
1@AlexR, just experienced what you had experienced "Once(many-many years ago)" – Raiyan Sep 26 '13 at 14:55
8 Answers
The only time a finally
block will not be executed is when you call exit()
before finally
is reached. The exit()
call will shutdown the JVM, so no subsequent line of code will be run.
EDIT: This is not entirely correct. See the comments below for additional information.

- 13,860
- 4
- 38
- 45
-
1Dunno if that's the *only* time. If `System.exit()` is fair game, isn't JNI code that segfaults? :-) – Mark Peters Aug 22 '11 at 06:34
-
17There are at least 3 OTHER cases where the `finally` block is not executed: 1) if the try block or a catch block goes into an infinite loop, or blocks for ever, 2) if something (e.g. a JNI bug) causes the JVM to crash, or 3) if there is a machine outage (power failure, hardware error, etc). – Stephen C Aug 22 '11 at 06:45
-
10@Stephen: #1 isn't really valid. It didn't fail to execute, it just hasn't yet. – Mark Peters Aug 22 '11 at 06:49
-
2Actually, if it is in a genuine infinite loop, it will NEVER execute unless the machine malfunctions. – Stephen C Aug 22 '11 at 12:00
-
It is better to say that exit() wherever used in your Java program will make the program to exit, no matter if you use it in a finally block or anywhere else. – sheidaei Apr 03 '13 at 18:32
The finally block will always execute no matter if you return, or an exception is thrown within the try block.
See also section 14.19.2 Execution of try-catch-finally of the Java Language Specification

- 13,351
- 8
- 59
- 84

- 68,052
- 28
- 140
- 210
The finally block gets executed in all these cases. The point of executing finally block at the end is to allow you to release all the acquired resources.
Below is an example that shows how it works.
public class Hello {
public static void hello(){
try{
System.out.println("hi");
return;
}catch(RuntimeException e){
}finally{
System.out.println("finally");
}
}
public static void main(String[] args){
hello();
}
}

- 4,610
- 2
- 31
- 54

- 99
- 1
- 8
It gets executed even if you return inside the try block. I put one return statement inside try as well as one inside finally and the value returned was from finally, not from try.
public class HelloWorld{
public static void main(String args[]){
System.out.println(printSomething());
}
public static String printSomething(){
try{
return "tried";
} finally{
return "finally";
}
}
}
The output I got was "finally."

- 388
- 1
- 4
- 15
According to the official explanation:
Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, 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.
I think it's a good idea that we should refer to the official website before we post an answer here.

- 1,316
- 15
- 19
When ever a method tries to return to the caller, via an uncaught exception or an explicit return statement, the finally clause is also executed just before the method returns.

- 1
If your finally block has a return, it will override any return in a try-catch block.Because of this "feature" a good practice is that finally block should never throw an exception or have a return statement.

- 747
- 1
- 12
- 24
If you call System.exit()
as someone else said, it will not be executed, but I believe it will also not be executed if there is an exception in the except block.

- 3,810
- 4
- 26
- 42
-
If an exception is thrown in a finally block, that implies execution reached the finally block. Perhaps the finally block will exit early due to the exception, but execution still gets there. – Andreas Jul 09 '14 at 17:52
-
That is true; I'm not extremely familiar with finally blocks, but if there is an exception in the finally block, doesn't the finally block simply stop execution and default to the state that it was in *before* so - essentially - it is the same as the finally block not being executed in the first place? :P – dylnmc Jul 09 '14 at 18:54
-
exceptions in the finally block are thrown up the stack till something catches them. The only difference is that if there is an exception in a try block, and a catch block tries to throw it up the stack, if a finally block runs and throws its own exception, that new exception is what gets thrown up the stack. Effectively this masks the underlining exception that was caught. The finally block never rewinds execution to a previous state nor fails to fire (outside of system.exit() getting called) – Andreas Jul 09 '14 at 20:00
-
Well if there's an exception in the "finally" block that means that "finally" block started executing. That's the whole point. What happens next in the "finally" block is not of importance for this question. – BJovke Nov 04 '16 at 10:06