Java throw

Summary: In this tutorial, you will learn how to use the Java throw statement to raise an exception explicitly in the program.

Introduction to the Java throw statement

The throw statement allows you to explicitly raise an exception within your code. The throw statement throws an exception explicitly and interrupts the normal flow of the program.

Here’s the syntax of the throw statement:

throw throwable;Code language: Java (java)

In this syntax, the throwable is an object of a class that extends the Throwable class.

Java throw statement example

The following example defines a method calculatePerimeterSquare() that calculates the perimeter of a square by its length:

public class App {
    public static double calculatePerimeterSquare(double length) {
        return 4 * length;
    }

    public static void main(String[] args) {
        var length = 10;
        var perimeter = calculatePerimeterSquare(length);
        System.out.println("The perimeter of the square is " + perimeter);
    }
}Code language: Java (java)

The problem is that you can pass zero or negative to the calculatePerimeterSquare() and get back an incorrect perimeter. For example:

public class App {
    public static double calculatePerimeterSquare(double length) {
        return 4 * length;
    }

    public static void main(String[] args) {
        var length = -10;
        var perimeter = calculatePerimeterSquare(length);
        System.out.println("The perimeter of the square is " + perimeter);
    }
}Code language: Java (java)

Output:

The perimeter of the square is -40.0Code language: Java (java)

To make the problem more robust, you need to validate the length argument in the calculatePerimeterSquare() method before calculating the perimeter.

The length is valid if its value is positive.

Also, you can throw an exception stating that zero or negative length is invalid.

The caller of the calculatePerimeterSquare() needs to handle the exception using the try catch statement.

The following program illustrates how to use the throw statement to raise an exception if the length is zero or negative:

public class App {
    public static double calculatePerimeterSquare(double length) {
        if (length <= 0) {
            throw new IllegalArgumentException("The length must be positive.");
        }
        return 4 * length;
    }

    public static void main(String[] args) {
        var length = -10;
        var perimeter = calculatePerimeterSquare(length);
        System.out.println("The perimeter of the square is " + perimeter);
    }
}Code language: Java (java)

In this example, we use the throw statement to raise an IllegalArgumentException with a message.

If you run the program and pass a negative value to the method calculatePerimeterSquare, you’ll get the IllegalArgumentException with a detailed message:

Exception in thread "main" java.lang.IllegalArgumentException: The length must be positiveCode language: Java (java)

To prevent the program from crashing, you can place the method call inside a try catch block like this:

public class App {
    public static double calculatePerimeterSquare(double length) {
        if (length <= 0) {
            throw new IllegalArgumentException("The length must be positive.");
        }
        return 4 * length;
    }

    public static void main(String[] args) {
        var length = -10;
        try {
            var perimeter = calculatePerimeterSquare(length);
            System.out.println("The perimeter of the square is " + perimeter);
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }
    }
}Code language: Java (java)

Output:

The length must be positive.Code language: Java (java)

Summary

  • Use the Java throw statement to throw an exception explicitly in your code.
Was this tutorial helpful ?