EDivByZero
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
EDivByZero is raised when your code divides an integer by zero at runtime. Delphi's CPU trap catches the illegal divide instruction and turns it into this exception. The fix is always to check that the divisor is not zero before performing the division.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Dividing an integer variable by another integer variable that happens to be zero
- A denominator that starts non-zero but is decremented or reset to zero before the division
- A function returning zero used directly as a divisor without a guard check
- Modulo operation (mod) with a zero right-hand operand — same trap as div
- User-supplied input used as a divisor without validation
How to Fix It
-
Run the program in the Delphi debugger. When EDivByZero fires, the debugger stops at the exact line. Note which variables are involved in the division.
The debugger shows local variable values in the Local Variables window, so you can see which operand is zero.
-
Add a zero-check before the division: if Denominator <> 0 then Result := Numerator div Denominator else Result := 0 (or handle the zero case appropriately).
Never divide and then check — the exception is raised the moment the division executes.
-
If the divisor comes from user input (an edit box, file, or database), validate it immediately when it is read: show an error message if the value is zero before it reaches any calculation.
Validating at the point of input prevents the bad value from ever reaching the calculation code.
-
Check modulo operations too. X mod Y raises EDivByZero when Y is zero, just like X div Y. Apply the same guard: if Y <> 0 then ... else ...
Modulo is often forgotten when searching for division-by-zero bugs.
-
If you need to catch the exception defensively, wrap the calculation in try/except: try Result := A div B; except on EDivByZero do Result := 0; end;
Exception handling is a safety net — fixing the root cause (never dividing by zero) is always preferred.
When to Call a Professional
EDivByZero is straightforward to fix once you find where the division occurs. Search your code for every use of div and mod and add a zero-check before each one. For floating-point division, Delphi raises EZeroDivide instead — make sure to handle both if you mix integer and float arithmetic.
Frequently Asked Questions
What is the difference between EDivByZero and EZeroDivide?
EDivByZero is raised for integer division by zero (using div or mod). EZeroDivide is raised for floating-point division by zero (using the / operator on Real, Double, or Single values). Both are subclasses of EMathError but they are separate exceptions — handle each one if you use both integer and float arithmetic.
Can I safely ignore EDivByZero with a blanket try/except?
You can catch it, but blindly suppressing it hides a real logic error in your program. A zero divisor usually means data is wrong or a calculation path was not anticipated. Always understand why the divisor is zero and fix the root cause rather than silently swallowing the exception.
Does EDivByZero happen with floating-point division?
No. Floating-point division by zero does not raise EDivByZero — it raises EZeroDivide. In fact, if floating-point exceptions are disabled, dividing a float by zero may return Infinity rather than raising any exception at all. EDivByZero is exclusively an integer arithmetic exception.