BOOST_CHECK_EXCEPTION( statement, exception, predicate )

This tool is used to perform an exception detection and validation check. The tool executes the supplied statement and check that it throw the supplied exception or it's child and that exception 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_CHECK_THROW this tool allows to perform more fine grained checks, for example that expected exception has specific error message etc.

If check is successful, the tool produces a confirmation message (note: to manage what messages appear in the test output stream set the proper log level). 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 tool's first parameter is the statement to execute while checking for exception. Use block statement if you want to execute more than one statement. The tool's second parameter is an expected exception. The last third parameter is a predicate function used to validate caught exception.

Example: test.cpp

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; }

int test_main( int, char* [] ) {
    BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception, is_critical );
    return 0;


test.cpp(9) : error in test_main: incorrect exception my_exception is caught

See Also