1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>BOOST_<level>_THROW</title> 5<link rel="stylesheet" href="../../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Boost.Test"> 8<link rel="up" href="../testing_tool_ref.html" title="Reference API for writing tests"> 9<link rel="prev" href="assertion_boost_level_no_throw.html" title="BOOST_<level>_NO_THROW"> 10<link rel="next" href="assertion_boost_level_exception.html" title="BOOST_<level>_EXCEPTION"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="assertion_boost_level_no_throw.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_throw"></a><a class="link" href="assertion_boost_level_throw.html" title="BOOST_<level>_THROW"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_THROW</span></code></a> 28</h4></div></div></div> 29<pre class="programlisting"><span class="identifier">BOOST_WARN_THROW</span><span class="special">(</span><span class="identifier">expression</span><span class="special">,</span> <span class="identifier">exception_type</span><span class="special">);</span> 30<span class="identifier">BOOST_CHECK_THROW</span><span class="special">(</span><span class="identifier">expression</span><span class="special">,</span> <span class="identifier">exception_type</span><span class="special">);</span> 31<span class="identifier">BOOST_REQUIRE_THROW</span><span class="special">(</span><span class="identifier">expression</span><span class="special">,</span> <span class="identifier">exception_type</span><span class="special">);</span> 32</pre> 33<p> 34 These assertions validate that the execution of <code class="computeroutput"><span class="identifier">expression</span></code> 35 raises an <span class="emphasis"><em>expected</em></span> exception, which means an exception 36 of the supplied <code class="computeroutput"><span class="identifier">exception_type</span></code> 37 type or of any child type. 38 </p> 39<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 40<li class="listitem"> 41 If <code class="computeroutput"><span class="identifier">expression</span></code> raises 42 an unexpected exception, this exception is not caught by <code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_THROW</span></code> assertion and might propagate 43 to the test body. If not caught at all, the framework will catch it 44 and terminate the test case with the status <span class="emphasis"><em>failed</em></span>. 45 </li> 46<li class="listitem"> 47 If <code class="computeroutput"><span class="identifier">expression</span></code> does 48 not raise any exception, the the assertion fails. 49 </li> 50</ul></div> 51<div class="warning"><table border="0" summary="Warning"> 52<tr> 53<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../../doc/src/images/warning.png"></td> 54<th align="left">Warning</th> 55</tr> 56<tr><td align="left" valign="top"><p> 57 the assertion catches only the expected exceptions. 58 </p></td></tr> 59</table></div> 60<div class="tip"><table border="0" summary="Tip"> 61<tr> 62<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td> 63<th align="left">Tip</th> 64</tr> 65<tr><td align="left" valign="top"><p> 66 It is possible to test for complex expressions with the use of constructs 67 such as <code class="computeroutput"><span class="keyword">do</span> <span class="special">{</span> 68 <span class="comment">/* ... */</span><span class="special">}</span> 69 <span class="keyword">while</span><span class="special">(</span><span class="number">0</span><span class="special">)</span></code> block. 70 </p></td></tr> 71</table></div> 72<h6> 73<a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_throw.h0"></a> 74 <span class="phrase"><a name="boost_test.utf_reference.testing_tool_ref.assertion_boost_level_throw.example_descr"></a></span><a class="link" href="assertion_boost_level_throw.html#boost_test.utf_reference.testing_tool_ref.assertion_boost_level_throw.example_descr">Example: 75 BOOST_<level>_THROW usage</a> 76 </h6> 77<div class="informaltable"><table class="table"> 78<colgroup><col></colgroup> 79<thead><tr><th> 80 <p> 81 Code 82 </p> 83 </th></tr></thead> 84<tbody><tr><td> 85<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span> 86<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 87 88<span class="keyword">class</span> <span class="identifier">my_exception</span><span class="special">{};</span> 89 90<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test</span> <span class="special">)</span> 91<span class="special">{</span> 92 <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 93 <span class="identifier">BOOST_CHECK_THROW</span><span class="special">(</span> <span class="identifier">i</span><span class="special">++,</span> <span class="identifier">my_exception</span> <span class="special">);</span> 94<span class="special">}</span> 95</pre> 96 </td></tr></tbody> 97</table></div> 98<div class="informaltable"><table class="table"> 99<colgroup><col></colgroup> 100<thead><tr><th> 101 <p> 102 Output 103 </p> 104 </th></tr></thead> 105<tbody><tr><td> 106<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">></span> <span class="identifier">example</span> 107<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span> 108<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">11</span><span class="special">):</span> <span class="identifier">error</span> <span class="identifier">in</span> <span class="string">"test"</span><span class="special">:</span> <span class="identifier">exception</span> <span class="identifier">my_exception</span> <span class="identifier">is</span> <span class="identifier">expected</span> 109 110<span class="special">***</span> <span class="number">1</span> <span class="identifier">failures</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">suite</span> <span class="string">"example"</span> 111</pre> 112 </td></tr></tbody> 113</table></div> 114<p> 115 See also: 116 </p> 117<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 118<li class="listitem"> 119 <a class="link" href="assertion_boost_level_no_throw.html" title="BOOST_<level>_NO_THROW"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_NO_THROW</span></code></a> 120 </li> 121<li class="listitem"> 122 <a class="link" href="../../testing_tools/exception_correctness.html" title="Exception correctness">Exception 123 correctness</a> section 124 </li> 125</ul></div> 126</div> 127<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 128<td align="left"></td> 129<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p> 130 Distributed under the Boost Software License, Version 1.0. (See accompanying 131 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 132 </p> 133</div></td> 134</tr></table> 135<hr> 136<div class="spirit-nav"> 137<a accesskey="p" href="assertion_boost_level_no_throw.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level_exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 138</div> 139</body> 140</html> 141