BOOST_WARN_EXCEPTION( statement, exception, predicate )
BOOST_CHECK_EXCEPTION( statement, exception, predicate )
BOOST_REQUIRE_EXCEPTION( statement, exception, predicate )

These tools are used to perform an exception detection and validation check. Tools execute the supplied statement and check that it throw the supplied exception (or one inherited from it) and that the exception object comply to the supplied predicate. If the statement throw any other unrelated exception, doesn't throw at all or predicate evaluates to false with caught exception, check fails. In comparison with BOOST_<level>_THROW tools these tools allow to perform more fine grained checks, for example that an expected exception has specific error message etc.

If check is successful, tools produce a confirmation message. In case if no exception thrown it produces an error message in a form "error in <test case name>: exception <exception> expected. In case if exception does not comply to specified predicate the tool produces an error message in a form "error in <test case name>: incorrect exception <exception> is caught.

The first parameter is the statement to execute. Use block statement if you want to execute more than one statement. The second parameter is an expected exception. The last third parameter is a predicate function used to validate caught exception.

Example: test.cpp

#define BOOST_TEST_MAIN
#include <boost/test/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; } BOOST_AUTO_TEST_CASE( test ) { BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception, is_critical ); }

Output:

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

*** 1 failure detected in test suite "Master Test Suite"

See Also

BOOST_CHECK_THROW