Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.


BOOST_WARN_EXCEPTION(expression, exception_type, predicate);
BOOST_CHECK_EXCEPTION(expression, exception_type, predicate);
BOOST_REQUIRE_EXCEPTION(expression, exception_type, predicate);

As for BOOST_<level>_THROW, these assertions validate that expression raises an exception of the type specified by exception_type or any of its child type, with additional checks on the exception instance.

predicate should be a unary function accepting the an instance of exception_type or any of its child, and that should return a boolean indicating the success (true) or failure (false).

[Warning] Warning

the assertion catches only the expected exceptions.

[Tip] Tip

It is possible to test for complex expressions with the use of constructs such as do { /* ... */} while(0) block.

The example below checks that the exception carries the proper error code.

Example: BOOST_<level>_EXCEPTION usage


#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

struct my_exception
  explicit my_exception( int ec = 0 ) : m_error_code( ec )

  int m_error_code;

bool is_critical( my_exception const& ex ) { return ex.m_error_code < 0; }

void some_func( int i ) { if( i>0 ) throw my_exception( i ); }

  BOOST_CHECK_EXCEPTION( some_func(1), my_exception, is_critical );


> example
Running 1 test case...
test.cpp(18): error in "test": incorrect exception my_exception is caught

*** 1 failures is detected in test suite "example"

See also: