🔴 The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
java.lang.ArithmeticException: / by zero⚡ Quick Fix Works 80% of the time
Check the divisor before dividing.
double avg = count == 0 ? 0.0 : (double) total / count;🧠 Why this Happens
Tap to expand the deep technical explanation
The idiv/irem/ldiv/lrem bytecodes trap divisor==0 for INTEGRAL operands and raise ArithmeticException("/ by zero") - the JVM refuses because integer division has no representable answer. Float and double divide instructions instead produce IEEE-754 Infinity/NaN per spec, which is why unit tests using doubles hide the bug until an int path executes in production.
The HITEC City Parking Spot Analogy:
Splitting ten mangoes among zero friends: arithmetic has no answer, so the kitchen halts instead of inventing one.
🔁 How to Reproduce Confirm this is your error
int q = 10 / 0; // ArithmeticException: / by zero int r = 10 % 0; // same message double f = 10.0 / 0.0; // NO exception -> Infinity
🛠️ Solutions (5 Ways to Fix)
Guard the divisor explicitly
👉 Use this if zero has a defined business meaning (empty set, no samples).
Branch on the divisor and return the domain-correct fallback so the operation stays total.
int percent = denom == 0 ? 0 : (num * 100) / denom;Validate denominators at the API boundary
👉 Use this if callers pass divisors into library-style methods.
Fail fast with a contextual exception naming the parameter, converting a bare runtime crash into actionable feedback.
static double ratio(double num, double denom) {
if (denom == 0) throw new IllegalArgumentException("denom must be nonzero");
return num / denom;
}Do money math in BigDecimal with scale and RoundingMode
👉 Use this if integer division silently truncating cents is the deeper bug.
BigDecimal.divide(divisor, 2, HALF_UP) avoids BOTH the truncation bug and its sibling NonTerminating exception by making precision explicit.
BigDecimal share = amount.divide(new BigDecimal("3"), 2, RoundingMode.HALF_UP);Switch to double deliberately, then guard results
👉 Use this if ratios feed charts/statistics where Infinity is unacceptable output.
IEEE doubles avoid the exception but emit Infinity; Double.isFinite checks catch that before it reaches UI or storage.
double rate = hits / attemptsDbl;
if (!Double.isFinite(rate)) rate = 0.0;Property-test divisor edges
👉 Use this if division logic sits in core calculation services.
Automated edge suites (0, MIN_VALUE, negatives) catch regressions that manual tests miss, especially MIN_VALUE/-1 overflow cases.
assertEquals(0, safeDiv(5, 0));
assertEquals(Integer.MIN_VALUE, safeDiv(Integer.MIN_VALUE, 1)); // no overflow path📋 Version Notes
Identical trap semantics; unchanged since Java 1.0.
Unchanged. Note MIN_VALUE / -1 overflows silently (returns MIN_VALUE) without throwing - a different hazard.
🛡️ How to Prevent This Next Time
Treat integral division as partial: every call site either guarantees nonzero denominators by construction or branches on zero explicitly.