Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Finally Block

When any return statement is encountered, any other statements will not be processed after that. Immediately control will be transferred to the caller of the method. In some cases, some statements are very important to be processed whether return statement is processed or not.
For example closing the resources. If you have used the resources and it’s not closed properly then it may create some problem. In order to make sure that the statement must be processed in any case in that you need to use the finally block.
When you are enclosing any statement inside the finally block then whether any return statement is encountered or not, any exception occurred or not, the finally block will always be processed.
A Finally block always works with try-finally or try-catch-finally pair.

            
class A{
	int m1(String ... arg){
		try{
			int a = Integer.parseInt(arg[0]);
			int b = Integer.parseInt(arg[1]);
			int c = a / b;
			return c;
		}catch(NumberFormatException e){
			System.out.println(e);
			return -1;
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("---"+e);
			return 0;
		}catch(ArithmeticException e){
			System.out.println(e);
			return 1;
		}finally{
			System.out.println("important codes are running");
		}
	}
}
class JTC{
	public static void main(String arg[]){
		int i = new A().m1(arg[0],arg[1]);
		System.out.println("i :- "+i);
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC 10 2
    important codes are running
    i :- 5

    C:\Users\Rahul\Desktop>java JTC 10 0
    java.lang.ArithmeticException: / by zero
    important codes are running
    i :- 1

    C:\Users\Rahul\Desktop>java JTC 10 abc
    java.lang.NumberFormatException: For input string: "abc"
    important codes are running
    i :- -1
            

In this example, as you can see, we have used the try-catch-finally format. We can see in the m1() method try block that first we are parsing string type data into an int type and then performing division operations, and we are handling all the possible exceptions that can occur using the catch block. There is also one finally block and a point that we must notice in the output screen from try and every catch block we are returning an int type value, but before returning the value to the caller, the first finally block gets executed.
It proves that when any return statement is encountered, then before returning the value, JVM checks if there is a finally block declared or not. If yes, then that finally block is processed first, then control moves back to try or catch the block and returns the specified value to the caller.
We can put the return statement in finally block if we have defined return statement in finally block then finally block value always gets return to the caller, the same concept we will see in next example.

            
class A{
	int m1(String ... arg){
		try{
			int a = Integer.parseInt(arg[0]);
			int b = Integer.parseInt(arg[1]);
			int c = a / b;
			return c;
		}catch(NumberFormatException e){
			System.out.println(e);
			return -1;
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("---"+e);
			return 0;
		}catch(ArithmeticException e){
			System.out.println(e);
			return 1;
		}finally{
			System.out.println("important codes are running");
			return 100;
		}
	}
}
class JTC{
	public static void main(String arg[]){
		int i = new A().m1(arg[0],arg[1]);
		System.out.println("i :- "+i);
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC 10 2
    important codes are running
    i :- 100

    C:\Users\Rahul\Desktop>java JTC 10 0
    java.lang.ArithmeticException: / by zero
    important codes are running
    i :- 100

    C:\Users\Rahul\Desktop>java JTC 10 abc
    java.lang.NumberFormatException: For input string: "abc"
    important codes are running
    i :- 100
            

In this example as we can see we have used return statement in finally block which is returning 100 to the caller, so we can see on the console in every case m1 method is returning 100 (value of i) weather we are getting any exception or not.

Ques:- Is there any way where finally block will not execute?

Yes, we have a built-in method in java.lang.System class which is exit (int) method. When exit(int) method executes then only finally block gets not executed, because when System.exit(int) method executes it terminates the JVM.

            
class A{
	int m1(String ... arg){
		try{
			int a = Integer.parseInt(arg[0]);
			int b = Integer.parseInt(arg[1]);
			int c = a / b;
			return c;
		}catch(NumberFormatException e){
			System.out.println(e);
			return -1;
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("---"+e);
			return 0;
		}catch(ArithmeticException e){
			System.out.println(e);
			System.exit(0);
			return 1;
		}finally{
			System.out.println("important codes are running");
		}
	}
}
class JTC{
	public static void main(String arg[]){
		int i = new A().m1(arg[0],arg[1]);
		System.out.println("i :- "+i);
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC 10 2
    important codes are running
    i :- 5

    C:\Users\Rahul\Desktop>java JTC 10 0
    java.lang.ArithmeticException: / by zero

    C:\Users\Rahul\Desktop>java JTC 10 abc
    java.lang.NumberFormatException: For input string: "abc"
    important codes are running
    i :- -1
            

In the above example as we can see we have used System.exit(0) in catch block which is associated with java.lang.ArithmeticException and we can see into the output when we are getting Arithmetic Exception then control is moving to the corresponding catch block and System.exit(0) method invokes that is the reason finally block is not executing.