ArithmeticException in Java – Causes & Treatment

In this tutorial, we will introduce a few examples that would highlight the causes of getting an ArithmeticException thrown within a Java program. We will discuss the common causes of the exception, and how these could be treated.

What is an ArithmeticException?

An arithmetic exception is an error that is thrown when an invalid arithmetic situation occurs. This usually happens when mathematical calculation errors occur within a program at run-time. Usually this is a mathematical situation which Java cannot deal with. There are various causes to an ArithmeticException which we will discuss in the following sections.

Dividing by a zero value integer

Java throws an Arithmetic exception when a calculation attempt is done to divide a number by zero, where the zero is an integer. Take the following piece of code as an example.

double x = 3/0; System.out.println("x = "+x);


When we run the code, we get the following error.

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at ArithmeticExceptionExamples.main(ArithmeticExceptionExamples.java:5)
Java Result: 1


Since we divided 3 by 0, where 0 is an integer, Java throws the above exception. However, if the zero is a floating point as in the following code, we get a completely different result.

double x = 3/0.0;
System.out.println("x = "+x);


Here, no exception is thrown, and “x” now has a value of infinity.

x = Infinity

Note that is (almost) always a bad idea to divide a number by zero in Java, even if no exception is thrown.

Non-terminating decimal numbers using BigDecimal

The BigDecimal class is a Java class used to represent decimal numbers up to a very high number of precision digits. The class also offers a set of functionalities that are not available using primitive data types such as doubles and floats. These functionalities include rounding up/down, defining the number of decimal places we need from a number etc.

Since BigDecimals are used to represent numbers to a high degree of accuracy, a problem might occur with some numbers, for example, when dividing 1 by 3, or 2 by 12. These numbers do not have a specific number of decimal places, for example, 1/3 = 0.33333333333333333… hence the term non-terminating.

Take the following program for example .

import java.math.BigDecimal;

public class ArithmeticExceptionExamples {

    public static void main(String[]args){
        
        BigDecimal x = new BigDecimal(1);
        BigDecimal y = new BigDecimal(3);
        x = x.divide(y);
        
        System.out.println(x.toString());
        
    }
}


If we run the program above, it might be surprising that we get the following result

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact 
representable decimal result.
	at java.math.BigDecimal.divide(BigDecimal.java:1603)
	at ArithmeticExceptionExamples.main(ArithmeticExceptionExamples.java:10)
Java Result: 1


The program doesn’t know what to do with the result of the division, hence an exception is thrown with the message “Non-terminating decimal expansion”. This is because the result does not have a finite number of decimal places.

One possible solution to the above problem is to set the number of decimal places we require from the result, and the form of rounding that will be used to limit the value to a certain number of decimal places. For example, we could say that z should be limited to 7 decimal places, by rounding down at the 7th decimal place.

import java.math.BigDecimal;

public class ArithmeticExceptionExamples {

    public static void main(String[]args){
        
        BigDecimal x = new BigDecimal(1);
        BigDecimal y = new BigDecimal(3);
        
        x = x.divide(y, 7, BigDecimal.ROUND_DOWN);//here we limit the # of decimal places
        
        
        System.out.println(x.toString());
        
    }
}


Here, the program runs properly, since we defined a limit to the number of decimal places the variable “x”  could assume.

0.3333333

Summary

In this tutorial, we discussed some interesting causes of an ArithmeticException in Java, and some solutions which could be practical when dealing with arithmetic calculations.

If you have any questions or if you liked this tutorial, then please let us know in the comments below, and make sure to subscribe to our twitter feed to be notified of the latest posts we publish.