Delphi 中ASSERT用法


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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM