BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_asserttestexception

Typedefs

typedef bsls::AssertTestException bsls_AssertTestException
 This alias is defined for backward compatibility.
 

Detailed Description

Outline

Purpose

Provide an exception type to support testing for failed assertions.

Classes

See also
bsls_assert, bsls_asserttest

Description

This component implements an exception class, bsls::AssertTestException, that provides a mechanism to convey context information from a failing assertion to a test handler. The context that is captured consists of the program source of the failing expression, the name of the file containing the assertion, the line number within that file where the asserted expression may be found, and the level of the assertion that has failed.

Note that this class is intended as an implementation detail of the bsls testing framework (see bsls_asserttest ), though it may be used in other contexts.

Usage

First we write a macro to act as an assert facility that will throw an exception of type bsls::AssertTestException if the asserted expression fails. The thrown exception will capture the source code, filename, and line number of the failing expression:

#define TEST_ASSERT(EXPRESSION) \$
if (!(EXPRESSION)) { \$
throw bsls::AssertTestException(#EXPRESSION, __FILE__, __LINE__, \$
"LEVEL"); \$
}
Definition bsls_asserttestexception.h:114

Next we use the macro inside a try-block, so that we can catch the exception thrown if the tested expression fails:

try {
void *p = NULL;
TEST_ASSERT(0 != p);
}

If the assertion fails, catch the exception and confirm that it correctly recorded the context of where the assertion failed:

catch (const bsls::AssertTestException& exception) {
assert(0 == strcmp("0 != p", exception.expression()));
assert(0 == strcmp(__FILE__, exception.filename()));
assert(9 == __LINE__ - exception.lineNumber());
assert(0 == strcmp("LEVEL", exception.level());
}
const char * level() const
Definition bsls_asserttestexception.h:214
const char * expression() const
Definition bsls_asserttestexception.h:202
const char * filename() const
Definition bsls_asserttestexception.h:208
int lineNumber() const
Definition bsls_asserttestexception.h:220

Typedef Documentation

◆ bsls_AssertTestException