Compiler Directive
Allows code in an include file to be incorporated into a Unit
1   {$I FileName}
2   {$I-}
3   {$I+}
The $I compiler directive covers two purposes. Firstly to include a file of code into the current unit. Secondly, to control whether exceptions are thrown when an IO error occurs.
Version 1
This is very useful for including compiler directives or common code into all of your units to ensure consistency, and a single point of control.
The default file name extension is .pas, so for .pas files, only the name is required. Use quotes if the name includes one or more spaces.
For example:
Common.pas file:
{$ReferenceInfo On}
{$RangeChecks On}
{$OverFlowChecks On}

Unit1.pas file:
{$I Common}

Versions 2 and 3
{$I+} default generates the EInOutError exception when an IO error occurs.
{$I-} does not generate an exception. Instead, it is the responsiblity of the program to check the IO operation by using the IOResult routine.
$I FileName is equivalent to $Include FileName.
$I- is equivalent to $IOChecks Off.
$I+ is equivalent to $IOChecks On.

This directive can be used multiple times within your code.
Related commands
$Include Allows code in an include file to be incorporated into a Unit
$IOChecks When on, an IO operation error throws an exception
IOResult Holds the return code of the last I/O operation
 Author links

 Download this web site as a Windows program.

Example code : Trapping IO exceptions, and using IOResult
// 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;
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  error : Integer;

  // Try to create a new subdirectory in the current directory
  // Switch off I/O error checking

  // Did the directory get created OK?
  error := IOResult;
  if error = 0
  then ShowMessage('Directory created OK')
  else ShowMessageFmt('Directory creation failed with error %d',[error]);

  // Try to create the directory again - this will fail!
  error := IOResult;
  if error = 0
  then ShowMessage('Directory created OK again')
  else ShowMessageFmt('Repeat creation failed with error %d',[error]);

  // Delete the directory to tidy up

  // Switch IO checking back on
Hide full unit code
   Directory created OK
   Repeat creation failed with error 183
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page