Delphi / Object Pascal · Exception
What does EInvalidCast Exception mean in Delphi / Object Pascal?
Raised when code performs an illegal typecast, most often an 'as' cast to a class or interface the object doesn't actually implement. It also fires from other hard-cast helpers when the runtime type check built into the cast fails at execution time.
Raised when code performs an illegal typecast, most often an 'as' cast to a class or interface the object doesn't actually implement. It also fires from other hard-cast helpers when the runtime type check built into the cast fails at execution time.
Replace an unconditional 'as' cast with an 'is' check first, or use Supports() for a safe interface cast that returns False instead of raising. When casting objects obtained generically - for example from a field list or component list - verify the actual class before casting.