Java Program to Handle Divide By Zero and Multiple Exceptions

Last Updated : 11 Sep, 2026

Exceptions are unexpected events that can interrupt the normal flow of program execution. Java provides exception-handling mechanisms to detect these situations and handle them without abruptly terminating the program.

  • ArithmeticException occurs when an invalid arithmetic operation, such as integer division by zero, is performed.
  • Multiple exceptions can be handled using multiple catch blocks or a multi-catch block using the | operator.

Example: Program to performs an arithmetic operation inside a try block and handles the ArithmeticException using a catch block.

Java
public class GFG {
    public static void main(String[] args) {

        int a = 10;
        int b = 0;

        try {
            int result = a / b;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Exception: Cannot divide by zero.");
        }
    }
}

Output
Exception: Cannot divide by zero.

Explanation: The program attempts to divide 10 by 0, which throws an ArithmeticException. The catch block handles the exception and displays an appropriate message instead of allowing the program to terminate abruptly.

Handling Multiple Exceptions in Java

Java provides two common approaches for handling multiple exceptions:

  • Using multiple catch blocks: Each exception type can have its own catch block and handling code.
  • Using a multi-catch block: Multiple exception types can be handled in a single catch block using the | operator.

1. Divide by Zero

When an integer is divided by zero, Java throws an ArithmeticException. The operation is invalid because integer division by zero is not defined.

Java
class DivideByZeroDemo {

    public static void main(String[] args) {

        int number = 12;
        int divisor = 0;

        // Attempting to divide by zero
        System.out.println(number / divisor);
    }
}

Output :

Exception in thread "main" java.lang.ArithmeticException: / by zero
at DivideByZeroDemo.main(DivideByZeroDemo.java:9)

Explanation: The value of divisor is 0. Therefore, the expression number / divisor attempts to perform integer division by zero. Java throws an ArithmeticException, and since the exception is not handled, the program terminates.

2. Handling Divide by Zero Using try-catch

The ArithmeticException can be handled using a try-catch block. The statement that may cause the exception is placed inside the try block, while the catch block provides the required handling code.

Java
class DivideByZeroDemo {

    public static void main(String[] args) {

        int number = 45;
        int divisor = 0;

        try {
            System.out.println(number / divisor);
        }
        catch (ArithmeticException e) {

            System.out.println(
                "Cannot divide an integer by zero");
        }
    }
}

Output
Cannot divide an integer by zero

Explanation: The division operation inside the try block causes an ArithmeticException because the divisor is zero. The exception is caught by catch (ArithmeticException), which prints a suitable message instead of allowing the program to terminate with an uncaught exception.

3. Handling Multiple Exceptions Using Multiple catch Blocks

A single try block can contain statements that may cause different types of exceptions. Java allows separate catch blocks to handle these exceptions individually.

Syntax:

try {
// Code that may cause an exception
}

catch (ExceptionType1 e) {
// Handles ExceptionType1
}

catch (ExceptionType2 e) {
// Handles ExceptionType2
}

Java
class MultipleCatchDemo {

    public static void main(String[] args)
    {

        try {
            int[] marks = new int[5];

            int result = 80 / 0;

            marks[5] = result;
        }
        catch (ArithmeticException e) {

            System.out.println(
                "Division by zero is not allowed");
        }
        catch (ArrayIndexOutOfBoundsException e) {

            System.out.println(
                "Array index is outside the valid range");
        }
    }
}

Output
Division by zero is not allowed

Explanation: The expression 80 / 0 causes an ArithmeticException. Once this exception occurs, the remaining statements inside the try block are skipped. Therefore, marks[5] is never executed and ArrayIndexOutOfBoundsException does not occur.

4. Handling Multiple Exceptions Using Multi-Catch

Java also provides a multi-catch feature that allows multiple exception types to be handled by a single catch block. The exception types are separated using the | operator.

Syntax:

try {
// Code that may cause an exception
}

catch (ExceptionType1 | ExceptionType2 e) {
// Common exception handling code
}

Java
class MultiCatchDemo {

    public static void main(String[] args) {

        try {
            int[] values = {10, 20, 30};

            // Invalid array index
            System.out.println(values[5]);
        }
        catch (ArrayIndexOutOfBoundsException
               | ArithmeticException e) {

            System.out.println(
                "Exception occurred: " + e.getMessage());
        }
    }
}

Output
Exception occurred: Index 5 out of bounds for length 3

Explanation: The values array contains three elements, so its valid indexes are 0, 1, and 2. Accessing index 5 causes an ArrayIndexOutOfBoundsException. The multi-catch block can handle either ArrayIndexOutOfBoundsException or ArithmeticException, so the exception is handled by the same catch block.

Comment