...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <exception>
#include <string>
class S : public std::exception {
std::string Mm;
public:
S(const char *msg) : Mm(msg) {}
const char *what() const noexcept override {
return Mm.c_str();
}
};
void g() {
// If some condition doesn't hold...
throw S("Condition did not hold");
}
void f() {
try {
g();
} catch (S &s) {
// Handle error
}
} |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdexcept>
#include <type_traits>
class S : public std::exception {
std::runtime_error Mm;
public:
S(const char *msg) : Mm(msg) {}
const char *what() const noexcept override {
return Mm.what();
}
};
static_assert(std::is_nothrow_copy_constructible<S>::value,
"S must be nothrow copy constructible");
void g() {
// If some condition doesn't hold...
throw S("Condition did not hold");
}
void f() {
try {
g();
} catch (S &s) {
// Handle error
}
} |
...
| SEI CERT C++ Coding Standard | ERR50-CPP. Do not call std::terminate(), std::abort(), or std::_Exit()abruptly terminate the program |
...