如何检测在析构过程中异常是否处于活动状态
How can I detect whether an exception is active during destructor?
在c++中,如何在析构函数体中检测堆栈是否由于抛出异常而展开?一旦检测到,我可以获得对活动异常的引用吗?
我问这个问题是因为我想添加一些调试代码,解释为什么会出现某种情况,以及是否由于异常。
std::uncaught_exception
告诉您是否由于抛出异常而展开堆栈,这是您所要求的。
然而,它并没有告诉你你可能想知道的:你调用它的析构函数的对象是在被展开的堆栈部分,还是在正常被取消的堆栈部分,因为非异常地退出了位于展开的其他析构函数下的作用域:
struct A {
~A();
};
struct B {
~B();
}
int main() {
try {
A a;
throw 1;
} catch(...) {}
}
A::~A() {
std::uncaught_exception(); // true
B b;
}
B::~B() {
std::uncaught_exception(); // also true, but "b" isn't being "unwound",
// because ~A() returned, it didn't throw.
}
与DeadMG和Xeo所说的相反,您无法获得对未被捕获的异常的引用。没有操作数的throw
重新抛出"当前处理的异常",也就是说,您所在的catch-handler或其catch-handler调用了您的异常。它不会重新抛出未捕获的异常。
c++ 17的新std::uncaught_exceptions()
呢?我认为你可以编写这样的代码:
class ExceptionSentinel
{
int prev_uncaught;
public:
ExceptionSentinel() : prev_uncaught(std::uncaught_exceptions()) {}
~ExceptionSentinel()
{
int cur_uncaught = std::uncaught_exceptions();
if (cur_uncaught > prev_uncaught)
{
// ... ExceptionSentinel is being destructed by an stack unwinding process
}
else
{
// ... Normal destruction of ExceptionSentinel
}
}
};
在本例中,std::uncaught_exceptions()
在调用代码之前跟踪未捕获异常的数量。
有一个std::uncaught_exception()
函数。但是,要尝试访问异常对象,您可以做的唯一有用的事情是重新抛出它并尝试捕获它。一般情况下,永远不要从析构函数抛出异常。
在c++中,如何在析构函数体中检测堆栈是否由于抛出异常而展开?
。
号一旦检测到,我可以得到对活动异常的引用吗?
相关文章:
- 是否删除在对象构造过程中创建的对象
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何在鼠标挂钩过程中检测拖动
- clang 插件:在编译过程中修改 AST
- Adafruit 羽毛RFM69HCW在使用过程中会冻结,需要硬重置
- 线程过程中的线程同步问题
- MySQL 连接器/C++.尝试在此过程中停止连接到数据库
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 在调用过程中删除 std::函数
- 动态矩阵特征分解过程中的误差
- cmake在构建过程中使用另一个工具检测标志
- 在模板参数推导过程中丢失限定符
- 升压过程中的管道缓冲区大小
- 如何在插入过程中更改插入的值
- 在PostgreSQL函数或过程中使用C++代码
- 在std::for_each的执行过程中累积结果,该异常被中断
- 在构建过程中捕获特定基础成员的异常
- 有人能确切地解释一下,如果在堆上分配对象数组的过程中抛出异常,会发生什么吗
- 如何检测在析构过程中异常是否处于活动状态
- 如果传递到 std::swap 的对象在交换过程中引发异常怎么办?