Home page

Sunday, June 11, 2023

Throw exception in JAVA

 public class ArithmeticExceptionExample {

    public static void main(String[] args) {

        int dividend = 10;

        int divisor = 0;


        try {

            int result = dividend / divisor;

        } catch (ArithmeticException e) {

            System.out.println("Caught exception: " + e.getMessage());

        }

    }

}


In this example, we divide dividend by divisor, where divisor is set to 0. This operation will throw an ArithmeticException due to division by zero. The catch block catches the exception and prints the exception message.

No comments:

Post a Comment