Example to throw ArithmeticException in Java

This article discuss what is an arithmetic exception and the why it occurs. Here you'll also learn how to handle such kind of exceptions using example.

Example to throw ArithmeticException in Java

This article discuss what is an arithmetic exception and the why it occurs. Here you'll also learn how to handle such kind of exceptions using example.

Example to throw ArithmeticException in Java

Example to throw Arithmetic Exception in Java

Arithmetic Exception is a kind of unchecked error in Java and these kind of exception occurs, when you divide a number by zero or if there is a mathematical or calculation errors that occurs at the RunTime.

java.lang.ArithmeticException is the base class of arithmetic exceptions that extends Runtime Exception.

See the example given below.

Syntax of Arithmetic Exception:

public ArithmeticException()
You can also write an ArithmeticException with the specified detail message, such as:
public ArithmeticException(String s)
Arithmetic Exception Example:
public class StringTest {
public static void divide() throws ArithmeticException {
int x = 10, y = 0;
int z = x / y;
} 
public static void main(String[] args) {
divide();
}
}

Output of the Exception:

Exception in thread "main" java.lang.ArithmeticException: / by zero