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.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值