Runtime Error 215
Delphi Programming
Severity: ModerateWhat Does This Error Mean?
Runtime Error 215 is an arithmetic overflow — an integer calculation produced a result larger than the variable type can hold. For example, multiplying two Integer values whose product exceeds 2,147,483,647, or adding 1 to a Byte that already equals 255. Delphi raises this error when overflow checking ({$Q+}) is enabled in the compiler options.
Affected Models
- Delphi 12 Athens
- Delphi 11 Alexandria
- Delphi 10.4 Sydney
- Delphi 10.3 Rio
- Delphi 10.2 Tokyo
Common Causes
- Multiplying two Integer values where the product exceeds the Integer maximum of 2,147,483,647
- Adding or subtracting values that push the result past the bounds of the integer type
- Intermediate calculations (not just the final result) overflowing their temporary type during evaluation
- Incrementing or decrementing a value that is already at the maximum or minimum for its type
- Using 32-bit Integer types in financial or scientific calculations that require 64-bit range
How to Fix It
-
Enable overflow checking during development. Add {$Q+} at the top of your unit or enable it globally in Project → Options → Compiler → Overflow checking. This makes Delphi detect overflowing integer calculations at runtime.
Like range checking, you can disable overflow checking ({$Q-}) for performance-critical release code, but always develop with it enabled.
-
Upgrade the integer type to one with a larger range. Replace Integer with Int64 for calculations that can produce values above about 2 billion. Replace Byte with Word or Cardinal when values may exceed 255.
Int64 range: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 — more than enough for most calculations.
-
Cast intermediate values to a wider type before the calculation, not after. For example: Result := Int64(A) * Int64(B); performs the multiplication in 64-bit space even if A and B are 32-bit integers.
Without the cast, A * B is computed as Integer multiplication first — overflowing — and then the overflowed result is widened to Int64. The overflow has already happened.
-
Add pre-calculation checks for operations that might overflow. Before multiplying A by B, check that A <= MaxInt div B (integer division) to confirm the result will fit.
MaxInt is a built-in Delphi constant equal to 2,147,483,647 (the maximum value of a 32-bit signed Integer).
-
For financial calculations, consider using the Currency type instead of Integer or Double. Currency is a 64-bit scaled integer type with 4 decimal places that avoids both integer overflow and floating-point rounding errors.
Currency is ideal for money values: it handles up to approximately ±922 trillion with 4 decimal places of precision.
When to Call a Professional
Runtime Error 215 is always fixable by using the correct integer type or restructuring the calculation. Enable overflow checking ({$Q+}) during all development builds to catch these issues early. For large number work, Int64 (range: ±9.2 × 10^18) is the right choice.
Frequently Asked Questions
What is the difference between Runtime Error 215 and Runtime Error 201?
Runtime Error 215 (arithmetic overflow) is triggered by {$Q+} and catches when a calculation itself overflows — the arithmetic operation produces an out-of-range result. Runtime Error 201 (range check error) is triggered by {$R+} and catches when you assign or use an out-of-range value — including assigning the result of a calculation to a variable of a narrower type. Both checks together provide complete protection: {$Q+}{$R+} is the recommended combination during development.
Why does integer overflow happen silently without overflow checking?
Without {$Q+}, integer arithmetic wraps around using modular arithmetic. For example, MaxInt + 1 gives -2,147,483,648 (the minimum Integer value) with no error at all. This silent wrap-around can produce negative values where positive ones are expected, incorrect loop counts, wrong financial totals, or security vulnerabilities. Always develop with {$Q+} enabled.
Should overflow checking be enabled in production builds?
For most applications, yes — the performance overhead is minimal (typically under 2%) and the safety benefit of catching overflow in production outweighs the cost. For performance-critical inner loops (large data processing, tight mathematical loops), you can wrap specific sections with {$Q-} ... {$Q+} to disable checking only where needed and only after you have verified correctness.