1.2.5 $C or $ASSERTIONS : Assertion support

The {$ASSERTIONS} switch determines if assert statements are compiled into the binary or not. If the switch is on, the statement

Assert(BooleanExpression,AssertMessage);

Will be compiled in the binary. If the BooleanExpression evaluates to False, the RTL will check if the AssertErrorProc is set. If it is set, it will be called with as parameters the AssertMessage message, the name of the file, the LineNumber and the address. If it is not set, a runtime error 227 is generated.

The AssertErrorProc is defined as

Type
  TAssertErrorProc=procedure(Const msg,fname : String;
                             lineno,erroraddr : Longint);
Var
  AssertErrorProc = TAssertErrorProc;

This can be used mainly for debugging purposes. The system unit sets the AssertErrorProc to a handler that displays a message on stderr and simply exits with a run-time error 227. The sysutils unit catches the run-time error 227 and raises an EAssertionFailed exception.