In this tutorial, we are going to write about ArithmeticException in Java, when it occurs and how to handle it.
ArithmeticException is an that happens at runtime; it is thrown when an exceptional arithmetic condition has occurred. The result of that is the Java Virtual Machine throwing a runtime exception because it can’t deal mathematically with the case at hand.
There are two main situations that lead to ArithmeticException:
- Dividing number by 0
- Using BigDecimal with infinite decimal representation
Dividing a number by 0 (integer zero) is a mathematically wrong operation, the result is undefined. It is possible to write code that might lead to that situation, for example:
public static void main(String[] args) {
System.out.println("Enter two numbers to divide them : a/b ");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int result = a/b;
System.out.println(result);
}
In the code above, we are asking the user to: enter two numbers, divide them and then print the result. Here the user can input anything, so if they enter “0” as denominator the program will throw an ArithmeticException.
We would then see something like the following in the standard output:
Enter two numbers to divide them : a/b
3
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.c1Test.main(c1Test.java:18)
The other case that leads to ArithmeticException is using a BigDecimal with infinite decimal representation.
Developers use BigDecimal to represent long decimal values since the type can contain 32 bit on an integer scale. It has quite a high precision. However, in the specific case when the decimal has an infinite series of digits, BigDecimal doesn’t have enough space to store the series. Let’s see the following example:
public static void main(String[] args) {
System.out.println("Enter two numbers to divide them : a/b ");
Scanner input = new Scanner(System.in);
Double a = input.nextDouble();
Double b = input.nextDouble();
BigDecimal decimal1 = new BigDecimal(a);
BigDecimal decimal2 = new BigDecimal(b);
decimal1 = decimal1.divide(decimal2);
System.out.println(decimal1.toString());
}
The above example is again asking the user two double values to divide the first by the second. Once the user enters the numbers, the code assigns these values to BigDecimal variables. To divide in BigDecimal we use the method .divide(), then we print the result. Let’s suppose that the user entered 1 and 7; that would produce a very long decimal representation and here’s how the language deals with it:
Enter two numbers to divide them : a/b
1
7
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1693)
at test.c1Test.main(c1Test.java:21)
How to handle the ArithmeticException?
Like any other exception in Java, developers can handle the ArithmeticException to ensure the program will not break when that happens. By using the try-catch block developers can handle this exception the way they want, let’s bring back the first example and handle it:
public static void main(String[] args) {
try {
System.out.println("Enter two numbers to divide them : a/b ");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int result = a / b;
System.out.println(result);
} catch (ArithmeticException exception) {
System.out.println("You cannot divide a number by 0!");
}
}
If the user enter “0” as the second number then the ArithmeticException will be handled by printing an error message that explains the situation.
Enter two numbers to divide them : a/b
3
0
You cannot divide number by 0 !