ArithmeticException: Division by Zero

java.lang.ArithmeticException: / by zero

Quick Answer

You are dividing an integer by zero. Check the divisor is not zero before performing the division.

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 zero

The Fix

int total = 100;
int count = 0;
int average = (count != 0) ? total / count : 0;

Step-by-Step Fix

  1. 1

    Identify the division

    Find the division or modulo operation on the failing line in the stack trace.

  2. 2

    Trace the zero divisor

    Find where the divisor variable gets its value and why it is zero.

  3. 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