• 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

  • 相关阅读:
    AOP
    关于zookeeper部署的个数
    Zookeeper 简介
    Java 正则表达式
    面试记录
    面试题
    Spring Framework官方文档翻译(中英文版)
    java知识巩固
    mysql sql记录
    redis入门
  • 原文地址:https://www.cnblogs.com/findumars/p/6311597.html
Copyright © 2020-2023  润新知