@@ -2651,13 +2651,6 @@ <h3 id="Exceptions">Exceptions</h3>
2651
2651
noted below.</ p >
2652
2652
2653
2653
< 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
-
2661
2654
< p class ="drake "> When documenting exceptions in an API, only state that
2662
2655
the function will throw < code > std::exception</ code > ; never promise a more
2663
2656
specific subclass:</ p >
@@ -2667,11 +2660,28 @@ <h3 id="Exceptions">Exceptions</h3>
2667
2660
to refactor the code in ways that keeps the subclass unchanged.</ li >
2668
2661
< li > There's no great way to deprecate a particular exception signature.</ li >
2669
2662
< 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 >
2673
2666
</ 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 >
2675
2685
</ div >
2676
2686
2677
2687
< p class ="pros "> </ p >
0 commit comments