The perfect place for easy learning...

Java Programming

×

Topics List


throw, throws, and finally keywords in Java





In java, the keywords throw, throws, and finally are used in the exception handling concept. Let's look at each of these keywords.

throw keyword in Java

The throw keyword is used to throw an exception instance explicitly from a try block to corresponding catch block. That means it is used to transfer the control from try block to corresponding catch block.

The throw keyword must be used inside the try blcok. When JVM encounters the throw keyword, it stops the execution of try block and jump to the corresponding catch block.

🔔 Using throw keyword only object of Throwable class or its sub classes can be thrown.

🔔 Using throw keyword only one exception can be thrown.

🔔 The throw keyword must followed by an throwable instance.


The following is the general syntax for using throw keyword in a try block.

Syntax
throw instance;

Here the instace must be throwable instance and it can be created dynamically using new operator.

Let's look at the following example Java code to illustrate throw keyword.

Example
import java.util.Scanner;

public class Sample {
                
    public static void main(String[] args) {
                    
        Scanner input = new Scanner(System.in);
        int num1, num2, result;
        System.out.print("Enter any two numbers: ");
        num1 = input.nextInt();
        num2 = input.nextInt();
        try {
            if(num2 == 0)
                throw new ArithmeticException("Division by zero is not posible");
            result = num1 / num2;
            System.out.println(num1 + "/" + num2 + "=" + result);
        }
        catch(ArithmeticException ae) {
            System.out.println("Problem info: " + ae.getMessage());
        }
            System.out.println("End of the program");		
    }
}

When we run the above code, it produce the following output.

throw keyword in java

throws keyword in Java

The throws keyword specifies the exceptions that a method can throw to the default handler and does not handle itself. That means when we need a method to throw an exception automatically, we use throws keyword followed by method declaration

🔔 When a method throws an exception, we must put the calling statement of method in try-catch block.


Let's look at the following example Java code to illustrate throws keyword.

Example
import java.util.Scanner;

public class ThrowsExample {
                
            
    int num1, num2, result;
    Scanner input = new Scanner(System.in);
                
    void division() throws ArithmeticException {
        System.out.print("Enter any two numbers: ");
        num1 = input.nextInt();
        num2 = input.nextInt();
        result = num1 / num2;
        System.out.println(num1 + "/" + num2 + "=" + result);
    }
                
    public static void main(String[] args) {
                    
        try {
            new ThrowsExample().division();
        }
        catch(ArithmeticException ae) {
            System.out.println("Problem info: " + ae.getMessage());
        }
        System.out.println("End of the program");		
    }
}

When we run the above code, it produce the following output.

throws keyword in java

finally keyword in Java

The finally keyword used to define a block that must be executed irrespective of exception occurence.

The basic purpose of finally keyword is to cleanup resources allocated by try block, such as closing file, closing database connection, etc.

🔔 Only one finally block is allowed for each try block.

🔔 Use of finally block is optional.


Let's look at the following example Java code to illustrate throws keyword.

Example
import java.util.Scanner;

public class FinallyExample {
            
    public static void main(String[] args) {
        int num1, num2, result;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter any two numbers: ");
        num1 = input.nextInt();
        num2 = input.nextInt();
        try {
            if(num2 == 0)
                throw new ArithmeticException("Division by zero");
            result = num1 / num2;
            System.out.println(num1 + "/" + num2 + "=" + result);
        }
        catch(ArithmeticException ae) {
            System.out.println("Problem info: " + ae.getMessage());
        }
        finally {
            System.out.println("The finally block executes always");
        }
        System.out.println("End of the program");	
    }
            
}

When we run the above code, it produce the following output.

throws keyword in java