Logic Error Catching
This topic applies to:
Edition |
Visual Basic |
C# |
C++ |
Web Developer |
---|---|---|---|---|
Express |
Native only |
|||
Standard |
Native only |
|||
Pro and Team |
Native only |
Table legend:
Applies |
|
Does not apply |
|
Command or commands hidden by default. |
You can use assertion statements to catch logic errors. You can set an assertion on a condition that must be true according to the logic of your program. The assertion has no effect unless a logic error occurs.
For example, suppose you are simulating gas molecules in a container, and the variable numMols represents the total number of molecules. This number cannot be less than zero, so you might include an MFC assertion statement like this:
ASSERT(numMols >= 0);
Or you might include a CRT assertion like this:
_ASSERT(numMols >= 0);
These statements do nothing if your program is operating correctly. If a logic error causes numMols to be less than zero, however, the assertion halts the execution of your program and displays the Assertion Failed Dialog Box.