Share via


MFC Assertions

This topic applies to:

Edition

Visual Basic

C#

C++

Web Developer

Express

Topic does not apply Topic does not apply

Native only

Topic does not apply

Standard

Topic does not apply Topic does not apply

Native only

Topic does not apply

Pro and Team

Topic does not apply Topic does not apply

Native only

Topic does not apply

Table legend:

Topic applies

Applies

Topic does not apply

Does not apply

Topic applies but command hidden by default

Command or commands hidden by default.

MFC defines the ASSERT macro for assertion checking. It also defines the MFC ASSERT_VALID and CObject::AssertValid for checking the internal state of a CObject-derived object.

The MFC ASSERT macro halts program execution and alerts the user if the argument (an expression) evaluates to zero or false. If the expression evaluates to a nonzero, execution continues.

When an assertion fails, a message dialog box displays the name of the source file and the line number of the assertion. If you choose Retry in the dialog box, a call to AfxDebugBreak causes execution to break to the debugger. At that point, you can examine the call stack and other debugger facilities to determine the cause of the assertion failure. If you have enabled Just-in-time debugging, the dialog box can launch the debugger if it was not running when the assertion failure occurred.

The following example shows how to use ASSERT to check the return value of a function:

int x = SomeFunc(y);
ASSERT(x >= 0);   //  Assertion fails if x is negative

You can use ASSERT with the IsKindOf function to provide type checking of function arguments:

ASSERT( pObject1->IsKindOf( RUNTIME_CLASS( CPerson ) ) );

The ASSERT macro catches program errors only in the Debug version of your program. The macro produces no code in the Release version. If you need to evaluate the expression in the Release version, use the VERIFY macro instead of ASSERT.

See Also

Concepts

Assertions