Boost C++ Libraries

...one 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.
PrevUpHomeNext

BOOST_<level>_EXCEPTION

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

These tools are used to perform an exception detection and validation check. Tools execute the supplied expression and validate that it throws an exception of supplied class (or the one derived from it) that complies with the supplied predicate. If the expression throws any other unrelated exception, doesn't throw at all or predicate evaluates to false, check fails. In comparison with BOOST_<level>_THROW tools these allow performing more fine-grained checks. For example: make sure that an expected exception has specific error message.

Example: BOOST_<level>_EXCEPTION usage

Code

#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_AUTO_TEST_CASE( test )
{
  BOOST_CHECK_EXCEPTION( some_func(1), my_exception, is_critical );
}

Output

> 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:


PrevUpHomeNext