RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Assert Function

Tests whether a Boolean expression is true.

Pascal
procedure Assert(expr: Boolean); overload;
procedure Assert(expr: Boolean; const msg: string); overload;
C++
Assert(Boolean expr);
Assert(Boolean expr, const AnsiString msg);

System

In Delphi code, use Assert as a debugging tool to test that conditions assumed to be true are never violated. Assert provides an opportunity to intercept an unexpected condition and halt a program rather than allow execution to continue under unanticipated conditions. 

Assert takes a Boolean expression and an optional message string as parameters. If the Boolean test fails, Assert raises an EAssertionFailed exception. If a message string was passed to Assert, the exception object is created with that string. Otherwise it is created with a default string indicating that the assertion failed. The message is displayed along with the complete path, filename, and the line number on which Assert failed. 

The SysUtils unit causes runtime errors to be turned into exceptions. If SysUtils is not used anywhere in your application, you will get a runtime error 227 rather than an EAssertionFailed exception. This runtime error will halt the program. 

Because assertions are not usually used in shipping versions of a product, compiler directives are provided to disable the generation of assertion code: 

$ASSERTIONS ON/OFF(long form) 

$C +/-(short form) 

These are global switches that affect the entire source file where they occur, regardless of their position in the file. It is not possible to enable and disable assertions for something smaller than a source file. Assertions are on by default. 

 

EAssertionFailed

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!