Use of assert in Delphi

Source: Internet
Author: User

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

Usage: ASSERT (expression)

If False, the assert generates an eassertionfailed exception that is displayed as

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

If you do not want to use these checks again, you can use ($ASSERTIONS OFF) or ($C-) Compile instructions

To invalidate an assert throughout the project, close Project Options | Compiler | Assertion option.

The use of the Delphi assert () function
The role of ASSERT (assert) is used for conditional testing. Expression expressions can be evaluated,

If the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.

The disadvantage is that frequent calls can greatly affect the performance of the program and add additional overhead.

Usage Summary and Precautions:

1) Verify the legitimacy of incoming parameters at the beginning of the function
2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed
3) You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into problems when it is actually running.
4) Assert and subsequent statements should be empty lines to create a logical and visual sense of consistency

5) In some places, assert cannot replace conditional filtering

What does the assert () function do?

To assert something, affirm something, is a debugging aid that, when asserted is violated, indicates a coding or design error (usually a coding error).

Assert accepts two parameters, one is the bool value, and the other is the literal of the exception that will be generated if the assertion is violated, the exception literal value can be omitted,

Like what

Procedure Tform1.somemethod (Aparam1:pointer);
Begin
According to the design, aParam1 cannot be nil
Asssert (aParam1 <> nil, ' aParam1 cannot be nil ');
Or
Asssert (aParam1 <> nil);

It can also be asserted that the execution of Somemethod,tform1 must be in some state
Asssert (not Visible);

...
End

The above code is equivalent to:

Procedure Tform1.somemethod (Aparam1:pointer);
Begin
{$IFOPT Assertions}
According to the design, aParam1 cannot be nil
If not (Aparam <> nil) Then
Asserterrorproc (' aParam1 cannot be nil ', Unitname, linenumber, erroraddr);
Or
If not (Aparam <> nil) Then
Asserterrorproc (", Unitname, LineNumber, erroraddr);
It can also be asserted that the execution of Somemethod,tform1 must be in some state
If not Visible and then
Asserterrorproc (", Unitname, LineNumber, erroraddr);
{$IFEND}

...
End

Note that it is not possible to use assert instead of raise Exception,assert is not part of the code.

That is, you can adjust the compilation options so that the final target code does not include the calculation of the assert bool expression,

Which means the entire assert function is removed.

So it is necessary to make this clear, although some statements are assertions of the state of the system, but the violation is not a coding cause,

For example, the user entered a string that is not expected, then can not use assert at this time, but should judge +raise exception

In Delphi code, use asserts as a debugging tool to test that conditions assumed to be true is 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 asserts, the exception object is created with the.

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 being turned into exceptions.

If sysutils isn't used anywhere in your application, you'll get a runtime error 227

Rather than an eassertionfailed exception.

This runtime error would halt the program.

Because assertions is not usually used in shipping versions of a product,

compiler directives is provided to disable the generation of assertion code:

$ASSERTIONS on/off (Long form)

$C +/ - (Short form)

These is 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 is on by default.

{This example exercises the System Assert function. The FirstCall passes and the second call fails.}typeTForm1=class(tform) Button1:tbutton; procedureButton1Click (Sender:tobject); Private    {Private Declarations}   Public    {Public Declarations}  End; Tstorage=class(TObject) FData:string;  PropertyData:string ReadFDataWriteFData; Private    {Private Declarations}   Public    {Public Declarations}  End; varForm1:tform1;Implementation{$R *.DFM}procedureModifystorage (astorage:tstorage;ConstSstring);beginAssert (Astorage<>Nil,"'); Astorage.data:=s;End;procedureTform1.button1click (sender:tobject);varStorage:tstorage;beginStorage:=tstorage.create; Trymodifystorage (Storage, ' Hello World '); finallyStorage.free; End; The following call isBuggy andtriggers the Assert modifystorage ( Nil, ' ooops ');End;

Use of assert in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.