Ad Space — Top Banner

ERangeError

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

ERangeError means a value has gone outside the valid range for its type. For example, assigning 300 to a Byte variable (which can only hold 0–255), or accessing an array index that is beyond the array's bounds. Delphi only catches this at runtime if range checking is enabled in the compiler options.

Affected Models

  • All Delphi versions
  • Range checking must be enabled ({$R+}) to get this at runtime

Common Causes

  • Assigning a value that is too large or too small for the target type (like 300 into a Byte)
  • Accessing an array index that is outside the declared bounds of the array
  • A loop counter or calculation producing a value outside the valid range for its variable type
  • Converting a large integer to a smaller integer type without checking the value first
  • Assigning a value outside the range of a subrange type or enumeration

How to Fix It

  1. Enable range checking during development. Add {$R+} at the top of your unit or enable it globally in Project → Options → Compiler → Range checking. This makes Delphi check every assignment at runtime.

    Disable {$R-} for release builds if performance is critical, but always develop with it on.

  2. Use the correct type for the value's expected range. If a value might exceed 255, use Word (0–65535) or Integer instead of Byte. Match the type to the data.

    Common Delphi integer types: Byte (0–255), Word (0–65535), Integer (-2B to +2B), Cardinal (0–4B).

  3. Before assigning or converting a value, check that it is within range. Use an if statement to validate the value before the assignment.

    Example: if (Value >= 0) and (Value <= 255) then MyByte := Value else raise Exception.Create('Value out of range');

  4. For array access, check that the index is within bounds before using it: if (Index >= Low(MyArray)) and (Index <= High(MyArray)) then ...

    Low() and High() return the first and last valid indexes of any array in Delphi.

  5. Review loop termination conditions. A loop that counts from 0 to Length(MyArray) will raise ERangeError on the last iteration. Use 0 to Length(MyArray) - 1, or use a for-in loop.

    For-in loops (for Item in MyArray) never have range issues — Delphi handles the bounds automatically.

When to Call a Professional

ERangeError is always something you can fix yourself. Add range checking ({$R+}) during development to catch these errors early. Use appropriate variable types — if a value can exceed 255, do not use Byte.

Frequently Asked Questions

Why does ERangeError only appear sometimes?

ERangeError only occurs at runtime if range checking ({$R+}) is enabled in the compiler options. With range checking off, out-of-range values silently overflow or truncate without any error. This can cause subtle data corruption bugs that are very hard to find. Always develop with range checking enabled.

What happens without range checking when a value overflows?

Without range checking, Delphi simply wraps the value around. For example, assigning 256 to a Byte gives 0 (256 mod 256 = 0). Assigning 257 gives 1. This is called integer overflow and produces silent wrong results — which are much harder to debug than an exception.

Should I keep range checking enabled in release builds?

It has a small performance cost but provides safety. For most applications, the performance impact is negligible and the safety benefit is worth it. For performance-critical code (like tight loops processing large data), you can wrap that specific section with {$R-} ... {$R+} to disable checking just there.