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

BOOST_WARN_THROW(expression, exception);
BOOST_CHECK_THROW(expression, exception);
BOOST_REQUIRE_THROW(expression, exception);

These tools are used to perform an exception detection check. Tools execute the supplied expression and validate that it throws an exception of supplied class (or the one derived from it) or it's child. If the statement throws any other unrelated exception or doesn't throw at all, check fails.

If check is successful, the tool produces a confirmation message, in other case it produces an error message in a form

error in <test-case-name>: exception <exception> expected

The first parameter is the expression to execute. Use do{} while(0) block if you want to execute more than one statement. The second parameter is an expected exception.

Example: BOOST_<level>_THROW usage

Code

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

class my_exception{};

BOOST_AUTO_TEST_CASE( test )
{
  int i =  0;
  BOOST_CHECK_THROW( i++, my_exception );
}

Output

> example
Running 1 test case...
test.cpp(11): error in "test": exception my_exception is expected

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

See also:


PrevUpHomeNext