...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdexcept>
class S {
bool shouldThrowshould_throw() const;
public:
~S() noexcept(false) {
// Normal processing
if (shouldThrowshould_throw()) {
throw std::logic_error("Something bad");
}
}
}; |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <exception>
#include <stdexcept>
class S {
bool shouldThrowshould_throw() const;
public:
~S() {
// Normal processing
if (shouldThrowshould_throw() && !std::uncaught_exception()) {
throw std::logic_error("Something bad");
}
}
}; |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdexcept>
class SomeClass {
class Bad {
bool shouldThrowshould_throw() const;
public:
~Bad() noexcept(false) {
if (shouldThrowshould_throw()) {
throw std::logic_error("Something bad");
}
}
};
Bad bad_member;
public:
~SomeClass()
try {
// ...
} catch(...) {
// Attempt to handle exceptions thrown from Bad destructor.
}
};
|
Compliant Code Example (try-block)
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <stdexcept> bool performDeallocperform_dealloc(void *); void operator delete(void *ptr) noexcept(false) { if (performDeallocperform_dealloc(ptr)) { throw std::logic_error("Something bad"); } } |
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <cstdlib> #include <stdexcept> bool performDeallocperform_dealloc(void *); void logFailurelog_failure(const char *); void operator delete(void *ptr) noexcept(falsetrue) { if (performDeallocperform_dealloc(ptr)) { logFailurelog_failure("Deallocation of pointer failed"); std::exit(1); // Fail, but still call destructors } } |
...