Last Updated on 10 July 2019   |   Print Email
This Java tutorial demystifies the try-catch-finally construct in Java programming language.Typically, a try-catch-finally construct looks like this:
try {
// code can throw exceptions
} catch (Exception ex) {
// exception hanlder
} finally {
// this block is always executed
}
The interesting point is that, code in the finally block always gets executed regardless of what happens in the try block. That means whether the exceptions throw or not, the Java Virtual Machine always execute code in the finally block. Hence the finally is preferred to clean up resources such as closing files, network connections, database connections and so on.Let’s see an example program that proves the characteristic of the finally block:
public class FinallyTest {
public static void main(String[] args) {
try {
String firstArg = args[0];
System.out.println("First Argument: " + firstArg);
} catch (IndexOutOfBoundsException ex) {
System.out.println("There is no argument");
} finally {
System.out.println("Finally gets executed");
}
}
}
Now, run this program using the following command line (no argument):java FinallyTestOutput:
There is no argument
Finally gets executed
And run it again with an argument passed from the command line:java FinallyTest Hello
Result:
First Argument: Hello
Finally gets executed
You see? Indeed, code in the finally block gets executed in both scenarios: exception and no exception.To understand clearly about the benefit of using finally block, consider the following program that writes some text to a file:
import java.io.*;
public class NameWriter {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("Name.txt");
writer.write("Hello "); //1
String name = args[0]; //2
writer.write(name); //3
writer.close(); //4
} catch (IOException e) {
e.printStackTrace();
}
}
}
Look, the line 4 will not be executed in case the line 2 causes an exception (IndexOutOfBoundsException if no argument is passed). If that happens, the try-catch block exits but the file remains opened as line 4 could not be reached.So a good practice is using the finally block to close the file regardless of exceptions, as shown in the following updated code:
- The only case in which the finally block does not get executed is when a call to System.exit() appears either in the try block or catch block. For example:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments