Java - ArithmeticException



The Java ArithmeticException throws an error when a wrong mathematical operation is performed.

In java, ArithmeticException is present in Java.lang package and the base class of this exception is java.lang.ArithmeticException. JVM raises an Arithmetic exception when the user tries to perform any arithmetic operations that are not possible.

There are some scenarios when JVM throws an Arithmetic exception in Java, two of them are following:

  • When the user tries to divide a number by zero, JVM throws an ArithmeticException.
  • When the user tries to perform any arithmetic operation that is not possible, JVM throws an ArithmeticException.

Constructors of ArithmeticException

There are two constructors of ArithmeticException class:

  • ArithmeticException(): This constructor is used to create an ArithmeticException object without any message.
  • ArithmeticException(String message): This constructor is used to create an ArithmeticException object with a message.

Methods of ArithmeticException

There are some methods of ArithmeticException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.

Example of ArithmeticException

In this example, we are dividing a number by zero, so JVM will throw an ArithmeticException.

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

Output

Following is the output of the above code:

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

As you can see in the output, JVM throws an ArithmeticException because we are dividing a number by zero.

Handling ArithmeticException

In this example, we are dividing a number by zero, so JVM will throw an ArithmeticException. We are handling this exception using try-catch block.

public class ArithmeticExceptionExample {
   public static void main(String[] args) {
      int a = 10;
      int b = 0;
      try {
         int c = a/b;
         System.out.println(c);
      } catch(ArithmeticException e) {
         System.out.println("Can't divide a number by zero");
      }
   }
}

Output

Following is the output of the above code:

Can't divide a number by zero

As you can see in the output, we are handling the ArithmeticException using try-catch block.

Throwing ArithmeticException explicitly

We will create an Arithmetic exception explicitly using the throw keyword. We can create a custom error using the throw keyword. This keyword is used to throw the exception for a method. It cannot throw multiple exceptions.

In the following example, we will throw the exception explicitly using the throw keyword. We will declare and initialize an array. Within the if block, if the sum of the 1st index and the 2nd index is less than 0 then it throws an exception otherwise print the value.

public class ArithmeticExceptionstring {
	
   public static void main(String[] args) {
      int [] arr = {4, -12, 6, 8};
      for (int i = 0; i < arr.length; i++){
	     if (arr[0]+arr[1] < 0){
			throw new ArithmeticException ("Negative value");
	     }else{
			System.out.println (arr[0]+arr[1]);
            break;
         }

      }
   }
}

Output

Following is the output of the above code:

Exception in thread "main" java.lang.ArithmeticException: Negative value
    at ArithmeticExceptionstring.main(ArithmeticExceptionstring.java:6)

As you can see in the output, we are throwing an ArithmeticException explicitly using the throw keyword.

java_lang_exceptions.htm
Advertisements