Delphi 中 断言 Assert 用法

procedure Assert(expr : Boolean [; const msg: string]);

用法:   Assert(表达式,[显示信息]);

如果为假, assert会产生一个EAssertionFailed异常,显示信息为:

Debugger Exception Notification

Project  Project2.exe raised exception  class EAssertionFailed with Message'AAA

(C:/Users/tangjianbao/Desktop/test/Project2.dpr line 23)'. Process stopped. use Step or Run to Contine.


 当你不想再使用这些检查时,可以使用 {$ASSERTIONS OFF/ON } 或 {$C-} 编译指令. 
要想使Assert在整个项目中失效,   关闭Project Options | Compiler | Assertion 选项。

Delphi7 Help  Code: (将下列代码拷贝 到 ConSole 可以直接运行,调试一下,体会会更深刻)

program Project2;

{$APPTYPE CONSOLE}

{ Defining OLDSTYLE for the compilation allows old-style
  runtime error handling to occur rather than the new
  exception-based method. If the old style is used, then
  the user-supplied string is not displayed. }

{$IFNDEF OLDSTYLE}
uses
  SysUtils;
{$ENDIF}

type
  TStorage = class(TObject)
    FData: string;
    property Data: string read FData write FData;
  end;

procedure ModifyStorage(AStorage: TStorage; const s: string);
begin
  Assert(AStorage <> nil, 'AAA');
  AStorage.Data := s;
end;

var
  Storage: TStorage;
begin
  Storage := TStorage.Create;
  try
    ModifyStorage(Storage, 'Hello world');
  finally
    Storage.Free;
  end;

  // The following call is buggy and triggers the Assert
  ModifyStorage(nil, 'Ooops');
 
end.

http://blog.csdn.net/tjb_1216/article/details/5601267

原文地址:https://www.cnblogs.com/findumars/p/6311597.html