Why This Happens
In Java, dividing an integer by zero throws ArithmeticException. Note that dividing a floating-point number by zero does not throw this exception; it produces Infinity or NaN instead. This commonly occurs when a denominator comes from user input or a calculation that can produce zero.
The Problem
int total = 100;
int count = 0;
int average = total / count; // ArithmeticException: / by zeroThe Fix
int total = 100;
int count = 0;
int average = (count != 0) ? total / count : 0;Step-by-Step Fix
- 1
Identify the division
Find the division or modulo operation on the failing line in the stack trace.
- 2
Trace the zero divisor
Find where the divisor variable gets its value and why it is zero.
- 3
Add a zero check
Check the divisor before dividing and handle the zero case with a default value or error message.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free