mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
* E.14 * clean-up * add final * Added words to dictionary * cleanup * fix typo * Update CppCoreGuidelines.md
This commit is contained in:
parent
369beb8a0c
commit
43f2e34148
|
@ -15992,80 +15992,56 @@ Sometimes, [`finally()`](#Re-finally) can make such unsystematic cleanup a bit m
|
||||||
|
|
||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
A user-defined type is unlikely to clash with other people's exceptions.
|
A user-defined type can better transmit information about an error to a handler. Information
|
||||||
|
can be encoded into the type itself and the type is unlikely to clash with other people's exceptions.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
void my_code()
|
throw 7; // bad
|
||||||
|
|
||||||
|
throw "something bad"; // bad
|
||||||
|
|
||||||
|
throw std::exception{}; // bad - no info
|
||||||
|
|
||||||
|
Deriving from `std::exception` gives the flexibility to catch the specific exception or handle generally through `std::exception`:
|
||||||
|
|
||||||
|
class MyException : public std::runtime_error
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
MyException(const string& msg) : std::runtime_error{msg} {}
|
||||||
// ...
|
// ...
|
||||||
throw Moonphase_error{};
|
};
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
void your_code()
|
// ...
|
||||||
{
|
|
||||||
try {
|
|
||||||
// ...
|
|
||||||
my_code();
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
catch(const Bufferpool_exhausted&) {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
##### Example, don't
|
throw MyException{"something bad"}; // good
|
||||||
|
|
||||||
void my_code() // Don't
|
Exceptions do not need to be derived from `std::exception`:
|
||||||
{
|
|
||||||
// ...
|
|
||||||
throw 7; // 7 means "moon in the 4th quarter"
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
void your_code() // Don't
|
class MyCustomError final {}; // not derived from std::exception
|
||||||
{
|
|
||||||
try {
|
|
||||||
// ...
|
|
||||||
my_code();
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
catch(int i) { // i == 7 means "input buffer too small"
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
##### Note
|
// ...
|
||||||
|
|
||||||
The standard-library classes derived from `exception` should be used only as base classes or for exceptions that require only "generic" handling. Like built-in types, their use could clash with other people's use of them.
|
throw MyCustomError{}; // good - handlers must catch this type (or ...)
|
||||||
|
|
||||||
##### Example, don't
|
Library types derived from `std::exception` can be used as generic exceptions if
|
||||||
|
no useful information can be added at the point of detection:
|
||||||
|
|
||||||
void my_code() // Don't
|
throw std::runtime_error("someting bad"); // good
|
||||||
{
|
|
||||||
// ...
|
|
||||||
throw runtime_error{"moon in the 4th quarter"};
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
void your_code() // Don't
|
// ...
|
||||||
{
|
|
||||||
try {
|
throw std::invalid_argument("i is not even"); // good
|
||||||
// ...
|
|
||||||
my_code();
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
catch(const runtime_error&) { // runtime_error means "input buffer too small"
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
**See also**: [Discussion](#Sd-???)
|
`enum` classes are also allowed:
|
||||||
|
|
||||||
|
enum class alert {RED, YELLOW, GREEN};
|
||||||
|
|
||||||
|
throw alert::RED; // good
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
Catch `throw` and `catch` of a built-in type. Maybe warn about `throw` and `catch` using a standard-library `exception` type. Obviously, exceptions derived from the `std::exception` hierarchy are fine.
|
Catch `throw` of built-in types and `std::exception`.
|
||||||
|
|
||||||
### <a name="Re-exception-ref"></a>E.15: Throw by value, catch exceptions from a hierarchy by reference
|
### <a name="Re-exception-ref"></a>E.15: Throw by value, catch exceptions from a hierarchy by reference
|
||||||
|
|
||||||
|
|
|
@ -343,6 +343,8 @@ Murray93
|
||||||
mutex
|
mutex
|
||||||
mutexes
|
mutexes
|
||||||
mx
|
mx
|
||||||
|
MyCustomError
|
||||||
|
MyException
|
||||||
myMap
|
myMap
|
||||||
MyMap
|
MyMap
|
||||||
myset
|
myset
|
||||||
|
|
Loading…
Reference in New Issue
Block a user