1 {$Q-}
2 {$Q+}
Description
The $Q compiler directive determines whether Delphi should add code to check for integer and enum operation value overflows.
This is set off by default, meaning that a bad integer or enum operation will pass unnoticed, revealing itself in a difficult to debug part of the code.
It is recommended to switch on OverFlowChecks in order to detect overflows before they cause problems. This will result in the raising of an exception, allowing code testing to correctlt identify the point of failure.
Notes
$OverFlowChecks is equivalent to $Q.
It can and should only be set once in your code.
The default value is $Q-.
Related commands
$OverFlowChecks Determines whether Delphi checks integer and enum bounds
Example code : Trapping overflow values
var
myNumber : Byte;
begin
// Set overflow checking on
{$Q+}
// A byte can hold numbers up to 255
myNumber := 255;
ShowMessage('myNumber = '+IntToStr(myNumber));
// But incrementing beyond 255 will throw an exception
Inc(myNumber);
ShowMessage('myNumber = '+IntToStr(myNumber));
end;
Show full unit code
myNumber = 255
Delphi throws the EIntOverflow exception
Example code : Ignoring overflow values
var
myNumber : Byte;
begin
// Set overflow checking off
{$Q-}
// A byte can hold numbers up to 255
myNumber := 255;
ShowMessage('myNumber = '+IntToStr(myNumber));
// But incrementing beyond 255 will wrap around to 0
Inc(myNumber);
ShowMessage('myNumber = '+IntToStr(myNumber));
end;
Show full unit code
myNumber = 255
nyNumber = 0