http://blog.csdn.net/dongyonggan/article/details/5780979

用法:ASSERT(表达式)

如果为假,ASSERT会产生一个EASSERTIONFAiled异常,显示为

Assertion Failed (C:/src/unit1.pas, [size=+0]line 34)

如果不想再使用这些检查时,可以使用($ASSERTIONS OFF)或($C-)编译指令

要想使Assert在整个项目中失效, 关闭Project Options | Compiler | Assertion 选项。

delphi assert()函数的用法
assert(断言)的作用是用来进行条件测试。可以计算表达式 expression ,

如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。

缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。

用法总结与注意事项:

1)在函数开始处检验传入参数的合法性
2)每个assert只检验一个条件,因为同时检验多个条件时,如果断言失败,无法直观的判断是哪个条件失败
3)不能使用改变环境的语句,因为assert只在DEBUG个生效,如果这么做,会使用程序在真正运行时遇到问题。
4)assert和后面的语句应空一行,以形成逻辑和视觉上的一致感

5)有的地方,assert不能代替条件过滤

请问Assert()函数是做什么用的?

表示断言某一件事,肯定某一件事,是一种调试辅助手段,当断言被违反时,则表明编码或者设计错误(通常是表明编码错误)。

assert接受两个参数,一个就是bool值,另一个是如果违反了断言将会产生的异常的字面,异常字面值可以省略,

比如

procedure TForm1.SomeMethod(aParam1: Pointer);
begin
  //按照设计,aParam1不能为nil
  Asssert(aParam1 <> nil, 'aParam1不能为nil');
  //或者
  Asssert(aParam1 <> nil);

//也可以断言,在执行SomeMethod,TForm1必定处于某种状态
  Asssert(not Visible);

...
end;

上面的代码相当于:

procedure TForm1.SomeMethod(aParam1: Pointer);
begin
  {$IFOPT ASSERTIONS}
  //按照设计,aParam1不能为nil
  if not (aParam <> nil) then
     AssertErrorProc('aParam1不能为nil', UnitName, LineNumber, ErrorAddr);
  //或者
  if not (aParam <> nil) then
     AssertErrorProc('', UnitName, LineNumber, ErrorAddr);
  //也可以断言,在执行SomeMethod,TForm1必定处于某种状态
  if not (not Visible) then
     AssertErrorProc('', UnitName, LineNumber, ErrorAddr);  
  {$IFEND}

...
end;

需要注意,不能用Assert代替raise exception,assert不是代码的一部分,

也就是,可以通过调整编译选项,使得最终目标代码中不包含assert的bool表达式的计算,

也就是整个assert函数被去掉

所以需要明确这点,虽然有些语句是属于对系统状态的断言,但是违反却不是编码造成的,

比如用户输入了不是期望的字符串,那么此时不能用assert,而应该用判断+raise exception

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.

{
This example exercises the System Assert function. The first
call passes and the second call fails.
} type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TStorage = class(TObject)
FData: string;
property Data: string read FData write FData;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure ModifyStorage(AStorage: TStorage; const s: string);
begin
Assert(AStorage <> nil, '');
AStorage.Data := s;
end; procedure TForm1.Button1Click(Sender: TObject);
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;

Delphi 中ASSERT用法的更多相关文章

  1. Delphi中Messagedlg用法

    if MessageDlg('Welcome to my Delphi application. Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrY ...

  2. Delphi中MessageBox用法

    消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.最简单用法,不带图形 MessageBox(0,'不同意','提示',MB_OK); MessageBo ...

  3. Delphi中stringlist分割字符串的用法

    Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...

  4. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

  5. Delphi中TxmlDocument控件的用法 转

    Delphi中对XML文件的解析做的很好,比直接使用MS的MSXML2_TLB中的接口要方便很多,现称述于下面. 在讲之前先给出一个XML实例,在讲某些部分是要结合实例比较容易理解. 1<?xm ...

  6. 转delphi中nil的用法

    转自:http://blog.csdn.net/haiou327/article/details/6666124 delphi中nil的用法 和C++中的NULL一样的意思,指空值,它和0值不一样-- ...

  7. Delphi中 StrToIntDef函数的用法

    Delphi中 StrToIntDef函数的用法:比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strtoi ...

  8. delphi中Application.MessageBox函数用法详解

    delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...

  9. Delphi中ClientDataSet的用法小结

    Delphi中ClientDataSet的用法小结 TClientDataSet控件继承自TDataSet,其数据存储文件格式扩展名为 .cds,是基于文件型数据存储和操作的控件.该控件封装了对数据进 ...

随机推荐

  1. Linux 入门记录:十九、Linux 包管理工具 RPM

    一.源代码管理 绝大多数开源软件都是直接以源代码形式发布的,一般会被打包为 tar.gz 的归档压缩文件.程序源代码需要编译为二进制可执行文件后才能够运行使用.源代码的基本编译流程为: ./confi ...

  2. Linux(Unix)密码策略问题导致root密码不能修改

    Linux(Unix)密码策略问题导致root密码不能修改 发布时间:  2016-01-19 浏览次数:  1034 下载次数:  5 用户修改了密码配置文件,导致root账户修改密码时报如下错误: ...

  3. jstorm相关

    https://www.cnblogs.com/antispam/p/4182210.html

  4. 【jzoj2017.8.21提高组A】

    太菜了,刷刷NOIP题玩玩. 今天的题好像以前有做过(雾) A. #include<bits/stdc++.h> typedef long long ll; ],cnt; ll x; in ...

  5. HDU 4768: Flyer

    题意: 有N个社团,每个社团三个属性A,B,C,表示会向编号A+k*C的同学发传单(k=0,1,2...  && A+k*C <= B).题目保证最多有一个人收到的传单数是奇数. ...

  6. django使用JWT保存用户登录信息

    在使用前必须弄明白JWT的原理,原理可以看我的另一篇博文:https://www.cnblogs.com/chichung/p/9966027.html JWT的流程 1.签发JWT 在用户正确输入账 ...

  7. 使用JAXB读写xml

    1.注解 在Java模型中的创建与 xml 文件对应的节点和属性需要用注解来表示 @XmlRootElement 作用:将一个Java类映射为一段XML的根节点参数:    name  定义这个根节点 ...

  8. es6解构赋值总结

    数组的解构赋值 1.简单的赋值方式 2.多维数组解构赋值 3.默认值,只有当右边对应位置为undefined时候才会选择默认(null不属于undefined) 4.左右不对等,会相应的对号入座,没有 ...

  9. 关于DRY原则

    软件工程,模式,语言,设计思想发展到今天,说白了,所有的技巧,思想,原则归根结底都是为了这个DRY  从机器语言开始: 为了DRY,出现了汇编符号来代表指令,使开发人员不用“重复翻阅指令手册” 为了D ...

  10. CodeForces 144B Meeting

    暴力. 题目只要求计算边上的点就可以了,一开始没看清题意,把内部的也算进去了.内部的计算可以延迟标记一下,但这题没有必要. #include<map> #include<set> ...