Divide by Zero Exception in Java

Muhammad Zeeshan Apr 07, 2022
  1. Divide by Integer Zero Exception in Java
  2. Divide by Floating Point Zero Exception in Java
Divide by Zero Exception in Java

This article will demonstrate what happens in a Java program when dividing by zero. Dividing by zero is an undefined operation since it has no significance in regular arithmetic.

While it is frequently connected with an error in programming, this is not necessarily the case. According to the Java division operation definition, we may look at a scenario of division by zero integers.

Divide by Integer Zero Exception in Java

Dividing a real integer by zero is a mathematical procedure that appears to be relatively easy yet lacks a clear and conclusive solution. Because any effort at definition leads to a contradiction, the outcome of this operation is technically deemed undefined.

Because this is a specific example of the division operation, Java recognizes it as an exceptional circumstance and throws the ArithmeticException whenever it comes across it during runtime.

public class dividebyzero {
    public static int divide(int f, int g) {
        int h = f / g;
        return h ;
    }
    public static void main(String... args) {
        int d = 8, r = 0;
        int e = divide(d, r);
        System.out.println(e);
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at dividebyzero.divide(dividebyzero.java:3)
    at dividebyzero.main(dividebyzero.java:8)

Resolve Divide by Integer Zero Exception in Java

The right approach to handle division by zero is to ensure that the divisor variable is never 0.

When the input cannot be controlled, and there is a potential of zero presenting itself in the equation, treat it as one of the expected choices and resolve it accordingly.

This normally entails checking the divisor’s value before using it, as shown below:

public class dividebyzero {
    public static int divide(int f, int g) {
        int h = f / g;
        return h ;
    }
    public static void main(String... args) {
        int d = 8, r = 0;
        if (r != 0) {
            int e = divide(d, r);
            System.out.println(e);
        } else {
            System.out.println("Invalid divisor: division by zero can't be processed)");
        }
    }
}

Output:

Invalid divisor: division by zero can't be processed)

Java includes a specialized exception named ArithmeticException to deal with unusual circumstances that emerge from computations.

When dealing with exceptional instances like integer division by zero, being very precise and careful is the key to avoiding the ArithmeticException.

Divide by Floating Point Zero Exception in Java

Floating-point values, by the way, also have -0.0; thus, 1.0/ -0.0 is -Infinity. Integer arithmetic lacks any of these values and instead throws an exception.

For example, unlike java.lang.ArithmeticException, the following case does not produce an exception when divided by zero. It expresses the infinite.

int x = 0;
double y = 3.2500;
System.out.println((y/x));

This is because you are working with floating-point numbers. Infinity is returned by division by zero, which is comparable to not a number or NaN.

You must test tab[i] before using it if you wish to avoid this. Then, if necessary, you can throw your own exception.

Java will not throw an exception whenever you divide by float zero. This will only notice a runtime bug when you divide by integer zero rather than double zero.

If you divide Infinity by 0.0, the outcome is Infinity.

0.0 is a double literal, and it is not regarded as absolute zero. There is no exception since the double variable is large enough to handle numbers representing approaching infinity.

You can use the line of code shown below to check for all possible values that could result in a nonfinite number, such as NaN, 0.0, and -0.0.

if (Math.abs(tab[i] = 1 / tab[i]) < Double.POSITIVE_INFINITY){
    throw new ArithmeticException("Result is non finite");
}

You may also check for yourself and then throw an exception.

try {
    for (int i = 0; i < tab.length; i++) {
        tab[i] = 1.0 / tab[i];
        if (tab[i] == Double.POSITIVE_INFINITY ||tab[i] == Double.NEGATIVE_INFINITY)
        { throw new ArithmeticException(); }
    }
    } catch (ArithmeticException xy) {
        System.out.println("ArithmeticException occured!");
}
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Java Exception