我知道从析构函数中抛出通常是一个坏主意,但我想知道我是否可以std::uncaught_exception()
用来安全地从析构函数中抛出。
考虑以下 RAII 类型:
struct RAIIType {
...
~RAIIType() {
//do stuff..
if (SomethingBadHappened()) {
//Assume that if an exception is already active, we don't really need to detect this error
if (!std::uncaught_exception()) {
throw std::runtime_error("Data corrupted");
}
}
}
};
这是c ++ 11中的UB吗?这是一个糟糕的设计吗?