Great work!

XP to next level

BugEater

Division by Zero

Learning Objectives

By the end of this lesson you will be able to:

  • Explain what happens when division by zero occurs in different languages
  • Select input values for any division operation including edge cases
  • Describe the correct application behavior when division by zero is attempted

Why Division by Zero is Undefined

In mathematics, division asks: "how many times does the divisor fit into the dividend?" If the divisor is zero, there's no meaningful answer — you can't fit something of zero size into any number.

Software must handle this. The approach varies by language and number type:

Language/Context Integer ÷ 0 Float ÷ 0
Java ArithmeticException Returns Infinity (IEEE 754)
JavaScript Returns Infinity Returns Infinity
Python ZeroDivisionError Returns inf
PostgreSQL Division by zero error Division by zero error
C/C++ Undefined behavior Returns Infinity

The Two Behaviors: Exception vs. Special Value

Exception path (integer division): The program crashes unless caught. Provides a clear signal — something went wrong.

Special value path (float division): The program continues, carrying Infinity forward. This is dangerous because the calculation doesn't crash — it produces a mathematically invalid result that gets passed to subsequent calculations.

double result = 1.0 / 0.0;  // = Infinity, no exception
double next = result + 5;    // = Infinity
double final = next * 2;     // = Infinity
// No error anywhere, but every result is wrong

Testing Division Operations: The Complete Test Suite

For any division form (dividend / divisor):

Test Case Dividend Divisor Expected
Normal division 10 2 5
Division by zero (int) 10 0 Error or rejection
Division by zero (float) 10.0 0.0 Error, or Infinity? (document which)
Zero divided by number 0 5 0
Zero divided by zero 0 0 Error (NaN territory)
Negative divisor 10 -2 -5

The Expected Behavior

A well-designed application should:

  1. Detect that the divisor is zero before performing the division
  2. Return a clear, user-friendly error message
  3. NOT pass Infinity or NaN to downstream calculations

A poorly designed application:

  • Crashes with HTTP 500 (unhandled exception)
  • Returns Infinity or NaN as a valid result to the user
  • Returns a wrong wrapped negative number

Pro Tip: Test division by zero as a standard case for any form that divides. Don't rely on the UI to prevent it — test it directly by sending divisor=0 to the backend.

Key Takeaways

  • Integer division by zero throws an exception; float division by zero returns Infinity (IEEE 754)
  • Infinity propagates through subsequent calculations silently — it's dangerous
  • The correct behavior: detect zero divisor, return a user-friendly error before computing
  • Test: divisor=0, dividend=0, both=0 as separate cases

Quiz

In Java, what happens when you execute int result = 10 / 0?

In JavaScript, what does 10 / 0 evaluate to?

A tester submits divisor=0 to a division API and receives HTTP 200 with body {"result": "Infinity"}. What should the tester conclude?

Which SQL query produces a database-level division-by-zero error?