What is throws clause?

The Java throws clause is used with methods to declare that the method may raise an error and it must be handled by the caller method. The throws clause is mostly used to handle the checked exception that transfers the exception to the caller method.

Method syntax

Following is the syntax for a Java method –

<modifier> <return-type> <method-name> (<parameter list>) <exception list> {
    // method body
}

If you have noticed then after parameter list there is a <exception list>, we place throws keyword with exception class names here.

Throws syntax

Now see the syntax for throws clause – 

throws exception_class1, exception_class2, exception_classN

With throws keyword, you can provide multiple exception classes.

Different examples

Following are the different examples to use throws clause, try and modify them to better understand.

Example 1

In the following example program throws clause is used with the main method to handle the checked exception.

import java.io.FileReader;
import java.io.IOException;

public class ThrowsExample1 {

    public static void main(String[] args) throws IOException {

        // It will raise FileNotFoundException if file is not found
        FileReader reader = new FileReader("Sample.txt");
    }
}

The FileReader class must be enclosed in a try-catch block but instead, we have used throws IOExcetion in the main method.

Example 2

In the following example program, throws clause is used with a method divide(int, int), that indicates the caller method to enclose it while calling.

public class ThrowsExample2 {

    public static int divide(int a, int b) throws Exception {

        if (b == 0) {
            throw new ArithmeticException("Cannot divide a number by 0");
        }

        return a / b;
    }

    public static void main(String[] args) {

        int numA = 1000;
        int numB = 0;

        try {

            // Exception must be handled
            int result = divide(numA, numB);
            System.out.println(result);

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }
}

Example 3

In the following example program, the method performDivision() should enclose method divide(int, int) in try-catch but instead, it transfers the exception to its caller method main() where it is handled using try-catch block.

public class ThrowsExample3 {

    public static int divide(int a, int b) throws Exception {

        if (b == 0) {
            throw new ArithmeticException("Cannot divide a number by 0");
        }

        return a / b;
    }

    public static void performDivision() throws Exception {

        int numA = 1000;
        int numB = 0;

        // Exception is transferd to caller method.
        int result = divide(numA, numB);
        System.out.println(result);
    }

    public static void main(String[] args) {

        try {

            // Exception is handled.
            performDivision();

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }
}

Example 4

The following example program shows how you can use multiple exception classes with throws keyword.

import java.io.IOException;

public class ThrowsExample4 {

    public static void test(String className) throws IOException, ClassNotFoundException {

        if (className == null) {
            throw new IOException("Invalid clasname provided!");
        }

        if (!className.equals("hellworld")) {
            throw new ClassNotFoundException("Invalid class provided!");
        }

    }

    public static void main(String[] args) {

        try {

            test("HelloWorld");

        } catch (IOException | ClassNotFoundException ex) {
            System.out.println(ex);
        }

    }
}