Try-catch-finally in java
Solution 1
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.
Solution 2
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
Solution 3
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();
}
}
Solution 4
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."
Related videos on Youtube
Nikhil
Updated on November 14, 2020Comments
-
Nikhil over 3 years
In Java, will the finally block not get executed if we insert a return statement inside the try block of a try-catch-finally ?
-
Mark Peters over 12 yearsWhen you tried it, what happened?
-
home over 12 yearsThe
finally
block will always get executed. That's why it's called finally :-) -
hansvb over 12 yearsWhatever you do, don't
return
from a finally block. -
AlexR over 12 years@Thilo, +1. Once (many-many years ago) I wrote return from finally and then spent half a day looking for the problem it caused...
-
Raiyan over 10 years@AlexR, just experienced what you had experienced "Once(many-many years ago)"
-
-
Mark Peters over 12 yearsDunno if that's the only time. If
System.exit()
is fair game, isn't JNI code that segfaults? :-) -
Stephen C over 12 yearsThere 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). -
Mark Peters over 12 years@Stephen: #1 isn't really valid. It didn't fail to execute, it just hasn't yet.
-
Stephen C over 12 yearsActually, if it is in a genuine infinite loop, it will NEVER execute unless the machine malfunctions.
-
sheidaei about 11 yearsIt 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.
-
Andreas almost 10 yearsIf 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.
-
dylnmc almost 10 yearsThat 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
-
Andreas almost 10 yearsexceptions 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)
-
Barranka almost 10 yearsIf possible, can you include a link to the reference of this?
-
Yu Zhang almost 8 yearswhere is the output?
-
BJovke over 7 yearsWell 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.