在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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()函数的用法 如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。 缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。 用法总结与注意事项: 1)在函数开始处检验传入参数的合法性 5)有的地方,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;
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论