Finally() Keyword in Java

Finally() Keyword in Java

In this article we will be discussing about Finally Keyword in Java.

The Finally keyword in Java is used to execute important code such as closing the connection and many more.

Finally() Keyword

Finally Keyword:

Finally, a block in Java which always gets executed in the compiler whether an exception is handled or not. So it contains all the necessary statements that needs to be printed when a program gets executed, regardless of whether an exception occurs or not.

There are 3 Different cases when we use Finally keyword, These are when:

  • There is no exception occur.
  • Exception occur which is handled by the catch block.
  • There is an exception occur without handled by the catch block.

Case 1: When there is no exception occur:

Run
import java.util.*;

public class Main{    
    public static void main(String args[]){
	// try block
        try{			
	    // try block Statements 
            int temp = 50/5;
            System.out.println(temp);
            System.out.println("I am in the try Block");    
        }
	// catch block
        catch(NullPointerException e){
			//catch block exceptions
            System.out.println(e);  
        }
	// finally block
        finally {  
	    // finally blick statement
            System.out.println("I am in finally block");  
        }
    }    
}
Output: 

10 I am in the try Block I am in finally block

Case 2: When there is an exception occur which is handled by the catch block :

Run
import java.util.*;

public class Main{    
    public static void main(String args[]){
	// try block
        try{
            System.out.println("I am in the try Block");    
            int temp = 50/0;
            System.out.println(temp);
        }
	//Catch Block
        catch(ArithmeticException e){  
            System.out.println(e);  
        }
	//finally block
        finally {  
            System.out.println("I am in finally block");  
        }
  }    
}
Output:

I am in the try Block
java.lang.ArithmeticException: / by zero
I am in finally block

Case 3 : When there is an exception occur without handled by the catch block.

Run
public class Main{    
    public static void main(String args[]){
        // try block    
        try{
            System.out.println("I am in the try Block");    
            int temp = 50/0;
            System.out.println(temp);
        }
        // catch block
        catch(NullPointerException e){  
            System.out.println(e);  
        }
        // finally block    
        finally {  
            System.out.println("I am in finally block");  
        }
  }    
}  
Output:

I am in the try Block
I am in finally block
Exception in thread "main" java.lang.ArithmeticException: / by zero at Prep.main(Prep.java:9)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription