__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.

An arithmetic exception is an error that is thrown, when an "wrong" arithmetic situation occurs. This usually happens when

mathematical or calculation errors occur within a program during run-time. There various causes to an ArithmeticException, the following

are a few of them:

a Java program. We will discuss the common causes of the exception, and how these could be treated.

An arithmetic exception is an error that is thrown, when an "wrong" arithmetic situation occurs. This usually happens when

mathematical or calculation errors occur within a program during run-time. There various causes to an ArithmeticException, the following

are a few of them:

__1 - Dividing by an integer Zero.__

Java throws an Arithmetic exception when a calculation attempt is done to divide by zero, where the zero is an integer. Take the following

piece of code as an example:

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.

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 by zero, even if no exception is thrown.

__2 - 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...

Take the following program for example :

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...

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()); } }

In the program above, since the class doesn't know what to do with the division result, an exception is thrown.

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

As we said, 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". One possible solution to the above problem is to define the number of decimal places we require

from a big decimal, and the form of rounding that will be used to limit the value to a certain number of decimales. For example, we could

say that z should be limited to 7 decimal places, by rounding up at the 7th decimal place:

"Non-terminating decimal expansion". One possible solution to the above problem is to define the number of decimal places we require

from a big decimal, and the form of rounding that will be used to limit the value to a certain number of decimales. For example, we could

say that z should be limited to 7 decimal places, by rounding up 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 places the variable "x" could assume.

0.3333333