java.lang.ArithmeticException: / by zero
Integer division or modulo by zero.
Seen on:
Java
Meaning
Integer division by zero throws; floating-point division returns Infinity/NaN instead. BigDecimal also throws for non-terminating decimals without a rounding mode.
Common causes
- Divisor is 0 (empty counts)
- BigDecimal.divide without scale/RoundingMode ("Non-terminating decimal expansion")
⚡ Quick fix
- Guard the divisor
- Use divide(x, 2, RoundingMode.HALF_UP) for BigDecimal
Detailed fix by platform
Java
BigDecimal avg = total.divide(BigDecimal.valueOf(n), 2, RoundingMode.HALF_UP);
How to diagnose
- Expression — Which division?
- Type — int or BigDecimal?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026