What Is Arithmetic exception In Java – Class java.lang. ArithmeticException

What Is Arithmetic Exception?

Thrown when an exceptional arithmetic condition has occurred. For example, an integer “divided by zero” throws an instance of this class.

This tutorial will look at a few examples highlighting the causes of getting an ArithmeticException in the Java program. Also, we will discuss the common causes of Arithmetic exceptions and how we can handle these exceptions.

An arithmetic exception is an error thrown when a “wrong” arithmetic situation occurs. This usually happens when mathematical or calculation errors occur within a program during run-time. There are various causes of an ArithmeticException, and the following are a few of them:

List of all invalid operations that throw an ArithmeticException() in Java

  • Dividing by an integer Zero
  • Non-terminating decimal numbers using BigDecimal

Dividing by an integer Zero:

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

package co.java.exception;
public class ArithmaticExceptionEx 
{
   void divide(int a,int b)
   {
      int q=a/b;
      System.out.println("Sucessfully Divided");
      System.out.println("The Value After Divide Is :-" +q);
   }
   public static void main(String[] args) 
   {
      ArithmaticExceptionEx obj=new ArithmaticExceptionEx();
      obj.divide(10, 0);
   }
}

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

Exception in thread "main" java.lang.ArithmeticException: / by zero at co.java.exception.ArithmaticExceptionEx.divide(ArithmaticExceptionEx.java:7) at co.java.exception.ArithmaticExceptionEx.main(ArithmaticExceptionEx.java:14)

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

Here, no exception is thrown, and “Q” now has a value of infinity.  Note that it is (almost) always a bad idea to divide by zero, 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 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 represent numbers accurately, 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 :

package co.java.exception;
import java.math.BigDecimal;
public class ArithmeticExceptionExamples {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      BigDecimal x = new BigDecimal(1);
        BigDecimal y = new BigDecimal(3);
        x = x.divide(y);       
        System.out.println(x.toString());
   }
}

Take a Look at the Execution:

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

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(Unknown Source) at co.java.exception.ArithmeticExceptionExamples.main(ArithmeticExceptionExamples.java:11)

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 decimals. For example, we could  say that z should be limited to 7 decimal places by rounding up at the 7th decimal place:

package co.java.exception;
import java.math.BigDecimal;
public class ArithmeticExceptionExamples {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   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());
   }
}

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

If you have any questions, please drop them in the Comment Section.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment