System.SysUtils.ERangeError

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

ERangeError = class(EIntError);

C++

class PASCALIMPLEMENTATION ERangeError : public EIntError

Properties

Type Visibility Source Unit Parent
class public
System.SysUtils.pas
System.SysUtils.hpp
System.SysUtils System.SysUtils

Description

ERangeError indicates an integer range violation.

ERangeError occurs in Delphi code when range checking is enabled and an ordinal value goes outside its declared range. By default, the Delphi compiler disables range checking. The compiler can always detect obvious range errors, which prevents the following code from compiling.

Note: Range checking is not a C++ feature. However, ERangeError can occur when C++ code calls Delphi code.

Example 1 (compile-time checking):

var
 SmallValue: 1 .. 3;
 SevenBits: byte;
begin
 SmallValue := 4; // E1012 Constant expression violates subrange bounds
 SevenBits := 256; // E1012 Constant expression violates subrange bounds
 // ...

The compiler cannot anticipate out-of-range values provided at run-time.


Example 2 (run-time checking):

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 SevenBits (high-order bits discarded)
 // ...

If in Example 2 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.

Range checking is considered a debugging feature. It produces bigger and slower executables.

See Also

Retrieved from "https://docwiki.embarcadero.com/Libraries/Tokyo/e/index.php?title=System.SysUtils.ERangeError&oldid=159676"