Sdílet prostřednictvím


Result Checking

This topic applies to:

Edition

Visual Basic

C#

F#

C++

Web Developer

Express

Topic does not apply Topic does not apply Topic does not apply

Native only

Topic does not apply

Pro, Premium, and Ultimate

Topic does not apply Topic does not apply Topic does not apply

Native only

Topic does not apply

You can use assertion statements to check the result of an operation. Assertions are most valuable for testing operations whose results are not obvious from a quick visual inspection.

For example, consider the following code, which updates the variable iMols based on the contents of the linked list pointed to by mols:

/* This code assumes that type has overloaded the != operator
 with const char * 
In addition, it also assumes that H2O is somewhere in that linked list. 
Otherwise we'll get an access violation... */
while (mols->type != "H2O")
{
 iMols += mols->num;
 mols = mols->next;
}
ASSERT(iMols<=numMols); // MFC version
_ASSERT(iMols<=numMols); // CRT version

The number of molecules counted by iMols must always be less than or equal to the total number of molecules, numMols. Visual inspection of the loop does not show that this will necessarily be the case, so an assertion statement is used after the loop to test for that condition.

See Also

Concepts

Logic Error Catching

Assertions