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.

If you fancy an assistant on your desk to entertain you, tell you about the weather or your appointments, then I recommend the Anki Cozmo – Collector’s Edition robot (click to check the price on Amazon). The Anki Cozmo will entertain you during those boring or lonely moments at work, or while studying, and will help you to keep your schedule and be more alert throughout the day.

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.


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


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.


Here, no exception is thrown, and “x” now has a value of 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 .


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


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.


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



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 would like to improve your knowledge about Java and how to write high performance code like a pro, then I would recommend reading Java Performance: The Definitive Guide: Getting the Most Out of Your Code (click to check current price on Amazon).

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.