Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection must be put inside the Java finally block. The Java finally block ensures that whatever statements are present in the finally block must be executed irrespective of the exception raised in the program or not. The Java finally can be used with the try-catch or can be used only with the try keyword.

Java finally syntax

The syntax of the finallykeyword is mentioned below.

Java finally Keyword Example

Let’s try to understand the usage of the keyword finally with the help of some examples.

FileName: FinallyExample.java

Output:

Explanation:The try-catch block handles the ArrayIndexOutOfBoundsException. After the try-catch block, the control shiftsto the finally block. Now observe the following example.

FileName: FinallyExample1.java

Output:

Explanation:In this program, we see that the ArithmeticException is raised in the try block. Even though the raised exception is not handled by the program, the finally block executes. It shows the importance of the finally block.  Any sensitive piece of code that has to be executedhas to be put only in the finally block. Notice the last print statement is not executed. It is because the raised exception is not handled, and that leads to the abnormal termination of the program.

Non-Execution of the Finally Block

Even though finally block ensures that some statements have to be executed, no matter what happens. However, there are some scenarioswhere even the finally block does not execute. The following program illustrates the same.

FileName: FinallyExample2.java

Output:

Explanation: In this program, we observe thatfinally block is not executed. Because of the statement System.exit(0);.The System.exit(0); statement stops the program by forcefully terminating the JVM (Java Virtual Machine). Hence, the finally block does notexecute.

Remember:

1) The finally block cannot exist independently. It has to come either with a try block or with a try-catch block.

2) The finally block always executes after the try block.

3) Presence of a catch block does not guarantee that the finally block executes after the catch block or not. The following program illustrates the same.

FileName: FinallyExample3.java

Output:

Explanation: In this program, we see the finally block is executed, and the catch block is not executed. The reason behind it is the type of exception raised in the program. The catch block is expecting the ArithmeticException to be raised in the try block. However, the try block raises the ArrayIndexOutOfBoundsException. Thus, there is a mismatch between what is expected and what is present. As the raised exception is not handled, the last print statemen also does not get executed, and the program terminates abnormally. In this example, we see that after the try block, the finally block gets executed, even though the catch block is present. Now, observe the following program.

FileName: FinallyExample4.java

Output:

Explanation: In this program, we see the finally block is executed after the catch block. It is because the raised exception is getting handled in the catch block.

4) The finally block does not guarantee that an exception cannot be raised in its block.

5) If the raised exception inside the finally block is not handled, the program terminates abnormally.

6) One can use try-catch inside the finally block to handle the raised exception inside the finally block. Observe the following program.

FileName: FinallyExample5.java

Output:

Explanation: The above program shows an exception can be raised as well as handled in the finally block.

Pin It on Pinterest

Share This