Ad Space — Top Banner

EListError

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

EListError is raised by Delphi list classes (TList, TStringList, TObjectList) when you access an index that is outside the valid range. For example, if a TStringList has 3 items (indexes 0, 1, 2), accessing index 3 or -1 raises EListError. The message is usually 'List index out of bounds (N)' where N is the invalid index you used.

Affected Models

  • All Delphi versions
  • TList, TStringList, TObjectList, and all TList descendants

Common Causes

  • Accessing an item by index when the list is empty (any index on an empty list is invalid)
  • Using an index equal to or greater than the Count of the list (indexes go from 0 to Count-1)
  • A loop that iterates one step too far — the off-by-one error
  • Deleting items from a list while iterating over it, causing the Count to change mid-loop
  • Using a hardcoded index that was valid when the code was written but the list now has fewer items

How to Fix It

  1. Before accessing an item by index, check that the index is valid: if (Index >= 0) and (Index < MyList.Count) then ...

    Or check the list is not empty first: if MyList.Count > 0 then MyList[0] is safe.

  2. When looping with an index, use 0 to MyList.Count - 1. Never use a hardcoded upper limit.

    Correct: for I := 0 to MyList.Count - 1 do — Incorrect: for I := 0 to 10 do (if the list has fewer than 11 items).

  3. When deleting items from a list while looping, iterate backwards. Loop from Count - 1 down to 0 so that deleting an item does not affect the indexes of items not yet processed.

    Example: for I := MyList.Count - 1 downto 0 do if NeedsDeleting(I) then MyList.Delete(I);

  4. Use for-in loops (for Item in MyList do) when you do not need the index. These are always safe — Delphi handles the bounds automatically.

    For-in also works correctly when the list is empty — it just does not execute the loop body.

  5. If the list might be modified by another thread while you are accessing it, use a critical section or a thread-safe list class to prevent race conditions that could change the Count unexpectedly.

    Multithreaded list access without synchronization can cause EListError and other unpredictable errors.

When to Call a Professional

EListError is always something you can fix yourself. The fix is to check the index before using it, or use a for-in loop to avoid indexes entirely. Never assume a list has a certain number of items without checking Count.

Frequently Asked Questions

What is the difference between EListError and ERangeError?

EListError is raised by Delphi list classes (TList, TStringList) when you use an invalid index. ERangeError is raised by the compiler's range checking when a value goes outside a type's valid range, such as accessing a fixed-size array out of bounds. Both indicate out-of-bounds access, but one comes from list objects and the other from the runtime range check system.

Why does the error message say 'List index out of bounds (-1)'?

An index of -1 usually means a search operation returned 'not found'. Delphi functions like IndexOf() and FindFirst() return -1 when the item is not found. If you use that -1 as an index, you get EListError. Always check if the returned index is >= 0 before using it.

Can I catch EListError and continue?

Yes, you can catch it with try/except. But it is better to prevent it with bounds checking. Catching exceptions for expected situations (like 'the list might be empty') is less efficient and less clear than just checking the count first.