ArithmeticException ☕ Java

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

  1. Guard the divisor
  2. Use divide(x, 2, RoundingMode.HALF_UP) for BigDecimal

Detailed fix by platform

Java

  1. BigDecimal avg = total.divide(BigDecimal.valueOf(n), 2, RoundingMode.HALF_UP);

How to diagnose

  1. Expression — Which division?
  2. 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.