ArithmeticException Handling in Java

April 21st, 2021 • By Rollbar Editorial Team

The java.lang.ArithmeticException is an unchecked exception in Java that occurs due to an exceptional arithmetic condition. This generally indicates that a mathematical error has occurred at run-time which can't be dealt with, for example, when an integer is divided by zero.

What Causes ArithmeticException

The java.lang.ArithmeticException can occur in the following situations:

  • When an integer is divided by zero
  • Overflowing of a data type when exact result is requested e.g. Math.addExact(int x, int y)
  • Rounding on an operation that yields an inexact result e.g. BigDecimal.ROUND_UNNECESSARY

ArithmeticException Example

Here is an example of a java.lang.ArithmeticException thrown due to division by zero:

public class ArithmeticExceptionExample {
public static void main(String[] args) {
	      int a = 10, b = 0;
	      System.out.println("Result: "+ a/b);
}
}

In this example, an integer a is attempted to be divided by another integer b, whose value is zero, leading to a java.lang.ArithmeticException:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at ArithmeticExceptionExample.main(ArithmeticExceptionExample.java:4)

Such operations should be avoided where possible by paying attention to detail and making sure all expressions in code are mathematically correct.

How to Handle ArithmeticException

The java.lang.ArithmeticException is an exception in Java, and therefore can be handled using try-catch blocks using the following steps:

  • Surround the statements that can throw an ArithmeticException in try-catch blocks
  • Catch the ArithmeticException
  • Depending on the requirements of the application, take necessary action. For example, log the exception with an appropriate message.

The code in the earlier example can be updated with the above steps:

public class ArithmeticExceptionExample {
	public static void main(String[] args) {
		try {
			int a = 10, b = 0;
			System.out.println("Result: " + a/b);
		} catch (ArithmeticException ae) {
			System.out.println("Arithmetic Exception: cannot divide by 0");
		}
		System.out.println("Continuing execution...");
	}
}

Surrounding the code in try-catch blocks like the above allows the program to continue execution after the exception is encountered:

Arithmetic Exception: cannot divide by 0
Continuing execution...

Track, Analyze and Manage Java Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage Java errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Try it today.

Get the latest updates delivered to your inbox.