ZeroDivisionError
What is ZeroDivisionError?
ZeroDivisionError: The code attempts to divide a number by zero, which is mathematically undefined. A ZeroDivisionError in Python, or DivisionByZeroError in PHP, happens when code divides a number by zero. In JavaScript, dividing by zero gives Infinity or NaN instead, which can quietly spread through later calculations. It usually appears in calculations based on data: an average over an empty list, a percentage of a total that is zero, or a conversion rate on a day without visitors.
Frequently asked questions about ZeroDivisionError
What should you keep in mind with ZeroDivisionError?
Handle the zero case explicitly, often by returning zero, null or a dash in reports, depending on what makes sense to the reader. Think about it whenever you divide by a count or total that comes from data. Tests with empty data sets catch these bugs early, before a dashboard breaks on the first quiet day after launch.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |