共用方式為


ENSURE (MFC)

Use to validate data correctness.

ENSURE(
   booleanExpression
)
ENSURE_VALID(
booleanExpression
)

Parameters

  • booleanExpression
    Specifies a boolean expression to be tested.

Remarks

The purpose of these macros is to improve the validation of parameters. The macros prevent further processing of incorrect parameters in your code. Unlike the ASSERT macros, the ENSURE macros throw an exception in addition to generating an assertion.

The macros behave in two ways, according to the project configuration. The macros call ASSERT and then throw an exception if the assertion fails. Thus, in Debug configurations (that is, where _DEBUG is defined) the macros produce an assertion and exception while in Release configurations, the macros produce only the exception (ASSERT does not evaluate the expression in Release configurations).

The macro ENSURE_ARG acts like the ENSURE macro.

ENSURE_VALID calls the ASSERT_VALID macro (which has an effect only in Debug builds). In addition, ENSURE_VALID throws an exception if the pointer is NULL. The NULL test is performed in both Debug and Release configurations.

If any of these tests fails, an alert message is displayed in the same manner as ASSERT. The macro throws an invalid argument exception if needed.

Example

void CMyClass::Open(CMyObject* pMyObject)
{
   ENSURE_VALID(pMyObject); 
   ENSURE(m_CubeLen > 0);
   pMyObject->DoSomething();
}

Requirements

Header: afx.h

See Also

Reference

VERIFY

ATLENSURE

Concepts

MFC Macros and Globals