Skip to content

Commit c68a09a

Browse files
committed
Clarify advice on throwing and testing for exceptions
1 parent edc9332 commit c68a09a

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

cppguide.html

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,13 +2651,6 @@ <h3 id="Exceptions">Exceptions</h3>
26512651
noted below.</p>
26522652

26532653
<div class="stylebody">
2654-
<p class="drake">Unit tests may catch exceptions using
2655-
<a href="https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#exception-assertions">EXPECT_THROW</a>
2656-
or
2657-
<a href="https://github.com/RobotLocomotion/drake/blob/master/common/test_utilities/expect_throws_message.h">DRAKE_EXPECT_THROWS_MESSAGE</a>,
2658-
but only if the tested function's Doxygen documentation mentions that the
2659-
function throws an exception.</p>
2660-
26612654
<p class="drake">When documenting exceptions in an API, only state that
26622655
the function will throw <code>std::exception</code>; never promise a more
26632656
specific subclass:</p>
@@ -2667,11 +2660,28 @@ <h3 id="Exceptions">Exceptions</h3>
26672660
to refactor the code in ways that keeps the subclass unchanged.</li>
26682661
<li>There's no great way to deprecate a particular exception signature.</li>
26692662
<li>Since we can't catch exceptions anyway, nevermind use a multi-layer-catch
2670-
by specific subclasses, the only purpose of a subclass is for improved error
2671-
reporting -- this purpose is still met by throwing a subclass, but we don't
2672-
need to document it in Doxygen for this purpose.</li>
2663+
by specific subclasses, the only purpose of choosing a specific subclass is
2664+
for improved error reporting -- this purpose is still met by throwing a
2665+
subclass, but we don't need to document it in the API for this purpose.</li>
26732666
</ul>
2674-
</p>
2667+
2668+
<p class="drake">When throwing and there is no particular reason to choose
2669+
any particular subclass of <code>std::exception</code> to be thrown, prefer
2670+
to use <code>std::logic_error</code> as our nominal concrete type. The
2671+
clarity of the error message string matters more than the subtype choice.</p>
2672+
2673+
<p class="drake">Unit tests may catch exceptions using
2674+
<a href="https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#exception-assertions">EXPECT_THROW</a>
2675+
or
2676+
<a href="https://github.com/RobotLocomotion/drake/blob/master/common/test_utilities/expect_throws_message.h">DRAKE_EXPECT_THROWS_MESSAGE</a>:</p>
2677+
<ul class="drake">
2678+
<li>Only test for exceptions under the conditions where the tested function's
2679+
API documentation promises that the function throws an exception.</li>
2680+
<li>Only test for a specific subtype of <code>std::exception</code> when the
2681+
subtype is <em>necessary</em> to determine whether the code behaved correctly,
2682+
e.g., in cases where the code may throw several different types of exception
2683+
and the subtype fully disambiguates which condition was triggered. (This is
2684+
rare.)</li>
26752685
</div>
26762686

26772687
<p class="pros"></p>

0 commit comments

Comments
 (0)