I have a try/catch with a return inside. Will the finally block execute?
try {
something();
return success;
}
catch (Exception e) {
return failure;
}
finally {
System.out.println("No se si esto se va a imprimir.");
}
I know I can write this and see what happens (which is really what I'm going to do) but when I googled it, nothing came up, so I asked the question here.
This is a question translated from the original in English and adapted to the results it gives on my computer: Does finally always execute in Java? jonny five
finally will be called.
The only times finally won't be executed are when:
This answer is a translation of jodonnell 's original English
Edited 2/12/15: Considering Does the finally block execute if the thread running the function is interrupted? by Subhrajyoti Majumder
A very simple question based on java principles, the finally block will always be called.
As long as as it is commented, you finish the execution of the code or there are errors.
More information: The Finally Block
NOT ALWAYS
The Java language specification describes how the try-catch-finally and try-finally blocks work in section 14.20.2 .
Nowhere does it specify that finally is always executed.
But it does specify that for all cases where the try-catch-finally or try-finally block completes before the finally block must have been executed.
Namely. If SIG is what will be executed after the try-catch-finally or try-finally block and FIN is what is inside finally, the JLS guarantees that FIN is always executed before SIG is executed .
Why doesn't the JLS guarantee that the finally block is always executed after the try block? Because it is impossible. It is unlikely but possible that the virtual machine will be aborted (kill, crash, shut down computer) just after the try block is finished and before the finally block is executed. And there is nothing that can be done from the language specification to avoid this.
Inside a block
try / catch
finally
it always executes, as the Java documentation says :Note: If the JVM exits while the try or catch code is executing, then the finally block cannot be executed . Likewise, if the thread executing the code is
try
eithercatch
interrupted or killed, the finally block cannot execute even though the application as a whole continues .As of Java SE 7 it `finally` lost prominence
Before Java SE 7 you could implement the use of
finally
as a key tool to prevent resource leaks . When closing a file or retrieving resources, it was recommended to put the code in a blockfinally
to ensure that the resource would always be retrieved or closed.Since it
finally
always executes, regardless of whether the statementtry
completes normally or abruptly, itfinally
was the only possible option to ensure that a resource would eventually be closed.We see this in the following example:
Before Java 7
finally
it helped us to leave the ground clean of all open resources.As of Java 7 the try-with-resources Statement has been implemented.
The statement
try-with-resources
is a statementtry
that declares one or more resources. A resource is an object that must be closed after the program has finished . The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implementsjava.lang.AutoCloseable
, which includes all objects that implementjava.io.Closeable
, can be used as a resource.The following example reads the first line from a file. Uses an instance of
BufferedReader
to read data from the file.BufferedReader
is a resource that should be closed after the program ends:In this example, the resource declared in the statement
try-with-resources
is aBufferedReader
. The declaration statement appears in parentheses immediately after the keywordtry
. The classBufferedReader
, in Java SE 7 and later, implements the interfacejava.lang.AutoCloseable
. Since the instanceBufferedReader
is declared in a statementtry-with-resources
, it will exit regardless of whether the try statement completes normally or abruptly (as a result of the methodBufferedReader.readLine
throwing aIOException
).