RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
ERangeError Class

ERangeError is the exception for Delphi range errors.

Pascal
ERangeError = class(EIntError);
C++
class ERangeError : public EIntError;

ERangeError occurs in Delphi code when range checking is enabled and an ordinal values goes outside its declared range.

Note: Range checking is not a C++ feature. However, ERangeError can occur when C++ code calls Delphi code.
Note: By default, the Delphi compiler disables range checking. The compiler can always detect obvious range errors, which prevents the following code from compiling.
//Delphi subrange example

var
  SmallValue: 1..3;
  SevenBits: byte;
begin;
  SmallValue := 4;  //Won't compile: constant expression out of bounds
  SevenBits := 256; //Won't compile: constant expression out of bounds

end;

Note: However, the compiler can't anticipate out-of-range values provided at run time:
//Delphi subrange example

var
  SmallValue: 1..3;
  SevenBits: byte;
  ThirtyTwoBits: Integer
begin;
  ThirtyTwoBits := 4;
  SmallValue := ThirtyTwoBits; //Assigns 4 to SmallValue (SmallValue is stored as a Byte)
  ThirtyTwoBits := 256;
  SevenBits := ThirtyTwoBits;  //Assigns 0 to ThirtyTwoBits (high-order bits discarded)

end;

Note: If range checking is enabled, both out-of-bounds assignments throw ERangeError exceptions. To enable range checking, use the Project option in the IDE or the $R+ directive.
Note: Range checking is considered a debugging feature. It produces bigger and slower executables.
Note: Do not confuse ERangeError with ERangeException, which relates to errors using graduated controls in VisualCLX.
 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!