Java finally Block

When the exceptions are thrown, execution in a method takes a rather abrupt, non-linear path that alters the normal flow by the method.

Depending upon the method is coded, it is even possible for an exception to cause the method to return ultimately. This could be a problem in several methods. For instance, if a method opens a file upon the entry and closes it upon the exit, then you will not want the code that closes the file to be bypassed by exception-handling mechanism. The keyword finally is designed to address this contingency.

The finally creates a block of code that will be executed after the try/catch block has completed and before the code following the try/catch block.

The finally block will execute always, whether an exception is thrown or not. If an exception is thrown, the finally block will execute even if no any catch statement matches the exception. Any time a method is about to return to the caller from inside the try/catch block, through an uncaught exception or an explicit return statement, the finally clause is also executed before the method returns. This can be useful in closing the file handles and freeing up any other resources that might have been allocated at the starting of a method with the intent of disposing of them before returning.

The finally clause is optional. Nevertheless, each try statement requires at least one catch or a finally clause.

Java finally Block Example

Following is an example program that shows the three methods that exit in various ways, none without executing their finally clauses :

/* Java Program Example - Java finally keyword
 * This program demonstrate the finally keyword 
 */
 
 class FinallyTest
 {
     /* Throw an exception out of method */
     static void procX() {
         try {
             
             System.out.println("inside procX");
             throw new RuntimeException("demo");
             
         } finally {
             
             System.out.println("procX's finally");
             
         }
     }
     
     /* return from within a try block */
     static void procY() {
         try {
             
             System.out.println("inside procY");
             return;
             
         } finally {
             
             System.out.println("procY's finally");
             
         }
     }
     
     /* execute a try block normally */
     static void procZ() {
         try {
             
             System.out.println("inside procZ");
             
         } finally {
             
             System.out.println("procZ's finally");
             
         }
     }
     
     public static void main(String args[])
     {
         try {
             
             procX();
             
         } catch(Exception e) {
             
             System.out.println("Exception caught");
             
         }
         
         procY();
         procZ();
     }
 }

In this program, the procX() method prematurely breaks out of the try by throwing an exception. The finally clause is executed on the way out. The method procY()'s try statement is existed via a return statement. The finally clause is executed before procY() returns. In the procZ(), the try statement executed normally, without an error. The finally block is however executed.

Below is the output generated by the above Java program :

java finally keyword

Note - If a finally block is linked with a try, the finally block will be executed upon conclusion of the try.

Java Online Test


« Previous Tutorial Next Tutorial »