Ruby Error

ZeroDivisionError

ZeroDivisionError is raised when attempting to divide an integer by zero. Note that dividing floats by zero returns Infinity instead of raising an error.

Common causes

  • Dividing by a variable that becomes zero
  • Calculating percentages with zero total
  • Array/collection operations resulting in zero divisor
  • User input resulting in zero

How to fix it

  • Check for zero before dividing: b != 0 ? a / b : 0
  • Use Float for Infinity behavior: a / b.to_f
  • Provide default values for zero cases
  • Validate input before calculations
  • Use rescue to handle the error gracefully

Example

ZeroDivisionError example
# Error example
10 / 0  # => ZeroDivisionError: divided by 0

# Fix with check
divisor = 0
result = divisor != 0 ? 10 / divisor : 0

# Float returns Infinity
10 / 0.0  # => Infinity

# Rescue approach
begin
  10 / divisor
rescue ZeroDivisionError
  0
end

Track ZeroDivisionError with Checkend

Checkend automatically captures ZeroDivisionError errors in your Ruby application with full context:

  • Complete backtrace with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.