12.1.1.3 Integer exception
An integer exception is derived from a Einterror class, but it is always raised in the program's subclass: Edivbyzero,erangeerror,eintoverflow.
Table 12.2 Integer anomalies and their causes
━━━━━━━━━━━━━━━━━━━━━
Exception class causes
─────────────────────
Edivbyzero tried to be 0 apart
Erangeerror integer expression out of bounds
Eintoverflow integer Operation overflow
━━━━━━━━━━━━━━━━━━━━━━
Erangeerror is thrown when the value of an integer expression exceeds the range allocated for a particular integer type. For example, the following code throws a Erangeerror exception.
Var
Smallnumber:shortint;
X, Y:integer;
Begin
X: = 100;
Y: = 75;
Smallnumber: = X * Y;
End
Specific integer types include Shortint, Byte, and integer-compatible enumeration types, Boolean types, and so on. For example:
Type
Thazard = (Safety, marginal, Critical, catastrophic);
Var
Haz:thazard;
Item:integer;
Begin
Item:= 4;
haz:= Thazard (Item);
End
A Erangeerror exception is thrown because the enumerator is out of bounds.
Array elements that are out of bounds also raise a erangeerror exception, such as:
Var
VALUES:ARRAY[1..10] of Integer;
I:integer;
Begin
For I: = 1 todo
Values[i]: = i;
End
Erangeerror exceptions are raised only when type checking is open. This can include {$R +} Compile instructions or set IDE option| in code The Range_checking option selection box for project.
Eintoverflow an exception class is raised when the integer, Word, Longint, or three integral types are out of bounds. Such as:
Var
I:integer;
A,b,c:word;
Begin
A: = 10;
B: = 20;
c: = 1;
For I: = 0 Todo
Begin
c: = A*b*c;
End
End
Throws a Eintoverflow exception.
The Eintoverflow exception class is only option| in the compilation selection box Project| Over_flow_check option is selected before it is generated. When the overflow check is turned off, the variable retains the maximum range value of the class integer after the overflow.
The range of integer types is the following table.
Table 12.3 Range of integer types
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Type range Format
───────────────────────────
Shortint-128.. 127 Signed 8-bit
Integer-32768.. 32767 signed 16-bit
Longint-2147483648.. 2147483647 signed 32-bit
Byte 0. 255 unsigned 8-bit
Word 0. 65535 unsigned 16-bit
━━━━━━━━━━━━━━━━━━━━━━━━━━━