Within a block try / catch
finally
always runs, as the documentation of Java :
The finally block is always executed when the block try
comes out. This
guarantees that the finally
block is executed even if a
unexpected exception. But, finally, it is useful for something more than the
exception handling: allows the programmer to prevent the code from
cleaning is accidentally diverted by a return, a
continuation or an interruption. Put cleaning code in a block
finally
is always a good practice , even when not foreseen
exceptions.
Note: If the JVM exits while the try or catch code is running, then the finally block can not be executed . Likewise, if the thread that executes the code try
or catch
is interrupted or deleted, the finally block can not be executed even if the application as a whole continues .
From Java SE 7 'finally' lost prominence
Before Java SE 7 you could implement the use of finally
as a key tool to avoid resource leakage . When closing a file or recovering resources, it was recommended to place the code in a finally
block to make sure that the resource will always recover or close.
Since finally
is always executed, regardless of whether the try
instruction completes normally or abruptly, finally
was the only possible option to ensure that a resource would be finally closed .
We see this in the following example:
//Otro uso de finally antes de Java SE 7
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
Before Java 7 finally
helped us to leave the land clean of all open resources.
As of Java 7, the try-with-resources Statement
was implemented
The try-with-resources
statement is a try
statement that declares one or more resources. A resource is an object that must be closed after the end of the program . The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable
, which includes all the objects that implement java.io.Closeable
, can be used as a resource.
The following example reads the first line of a file. Uses an instance of BufferedReader
to read data from the file. BufferedReader
is a resource that must be closed after the end of the program:
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
In this example, the resource declared in the try-with-resources
statement is a BufferedReader
. The declaration statement appears in parentheses immediately after keyword try
. The class BufferedReader
, in Java SE 7 and later, implements the interface java.lang.AutoCloseable
. Since instance BufferedReader
is declared in a try-with-resources
statement, will be closed regardless of whether the try statement completes normally or abruptly (as a result of the BufferedReader.readLine
method that throws a IOException
).