An ArithmeticException is an Exception that indicates an error generated when performing a calculation.
An ArithmeticException
is an Exception
that indicates an error generated when performing a calculation.
A common calculation that throws this exception is trying to divide a number by zero, such as this...
double firstNumber = 10;
double secondNumber = input.readDouble();
double result = firstNumber / secondNumber;
When coding such a calculation, it is easy to overlook the fact that the user can input a zero
for the secondNumber
. For the vast majority of numbers, this code will run correctly, and so the programmer might overlook the potential problem.
Programmers should wrap calculation code within try-catch blocks to help catch these Exceptions, or perform validation of variables before they're used in a calculation.