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 |
|
|
|
|
Example code : Trapping overflow values |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses SysUtils, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); 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; end.
|
Hide 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
|
|