×
☰ See All Chapters

Java finally block

The finally block is a block that is always executed. It is mainly used to perform some important tasks such as closing connection, closing stream etc.

java-finally-block-0
 

Note: Before terminating the program, JVM executes finally block (if any). finally must be followed by try or catch block.

Why use finally block?

finally block can be used to put "cleanup" code such as closing a file, closing connection , to release resources etc…

Case 1: Program in case exception does not occur

class ExceptionHandling {

        public static void main(String args[]) {

                try {

                        int data = 25 / 5;

                        System.out.println(data);

                } catch (NullPointerException e) {

                        System.out.println(e);

                } finally {

                        System.out.println("finally block is always executed");

                }

                System.out.println("rest of the code...");

        }

}

 

Output:

5

finally block is always executed

rest of the code...

Case 2: Program in case exception occurred but not handled

class ExceptionHandling {

        public static void main(String args[]) {

                try {

                        int data = 25 / 0;

                        System.out.println(data);

                } catch (NullPointerException e) {

                        System.out.println(e);

                } finally {

                        System.out.println("finally block is always executed");

                }

                System.out.println("rest of the code...");

        }

}

Output:

finally block is always executed

Exception in thread "main" java.lang.ArithmeticException: / by zero

        at com.manum.hassan.ExceptionHandling.main(ExceptionHandling.java:93)

Case 3: Program in case exception occured and handled

class ExceptionHandling {

        public static void main(String args[]) {

                try {

                        int data = 25 / 0;

                        System.out.println(data);

                } catch (ArithmeticException e) {

                        System.out.println(e);

                } finally {

                        System.out.println("finally block is always executed");

                }

                System.out.println("rest of the code...");

        }

}

Output:

java.lang.ArithmeticException: / by zero

finally block is always executed

rest of the code...

Rule: For each try block there can be zero or more catch blocks, but only one finally block.

Note: The finally block will not be executed if program exits (either by calling System.exit() or by causing a fatal error that causes the process to abort).

What are the different conditions that a method gets terminated?

There are three conditions or reasons for which method gets terminated.

  1. return keyword. (control returns to calling line of the caller method) 

  2. An exception occurs (control returns to the catch block of the caller method) 

  3. System.exit() method (program terminates) 

 


All Chapters
Author