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

PrevUpHomeNext

BOOST_<level>_THROW

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

These assertions validate that the execution of expression raises an expected exception, which means an exception of the supplied exception_type type or of any child type.

[Warning] Warning

the assertion catches only the expected exceptions.

[Tip] Tip

It is possible to test for complex expressions with the use of constructs such as do { /* ... */} while(0) block.

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