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

BOOST_WARN_NO_THROW(expression);
BOOST_CHECK_NO_THROW(expression);
BOOST_REQUIRE_NO_THROW(expression);

These tools are used to perform a "no throw" check. Tools execute the supplied expression and validate that it does not throw any exceptions. Error would be reported by the framework even if the statement appear directly in test case body and throw any exception. But these tools allow proceeding further with test case in case of failure.

If check is successful, tools may produce a confirmation message, in other case they produce an error message in a form

error in <test-case-name>;exception was thrown by <expression>

The only parameter is an expression to execute. You can use do {} while(0) block if you want to execute more than one statement.

Example: BOOST_<level>_NO_THROW usage

Code

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

class my_exception{};

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

BOOST_AUTO_TEST_CASE( test )
{
    BOOST_CHECK_NO_THROW( some_func(-1) );
}

Output

> example
Running 1 test case...
test.cpp(10): error in "test": exception thrown by throw my_exception()

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

See also:


PrevUpHomeNext