gpt4 book ai didi

c++ - 是否可以同时有两个或多个事件异常?

转载 作者:可可西里 更新时间:2023-11-01 17:37:45 24 4
gpt4 key购买 nike

C++17 引入了一个新函数 std::uncaught_exceptions :

Detects how many exceptions have been thrown or rethrown and not yet entered their matching catch clauses.

以下代码:

#include <iostream>

using namespace std;

struct A
{
~A()
{
cout << std::uncaught_exceptions() << endl;
}
};

int main()
{
try
{
try
{

A a1;
throw 1;
}
catch (...)
{
A a2;
throw;
}
}
catch (...)
{
A a3;
}
}

输出:

1

1

0

是否可以同时有两个或多个活跃异常?

有没有例子?

最佳答案

是的。从因堆栈展开处理另一个异常而调用的析构函数中抛出异常:

struct D
{
~D()
{
std::cout << std::uncaught_exceptions() << std::endl;
}
};

struct E
{
~E()
{
try
{
D d_;
throw 2;
}
catch(...)
{
std::cout << std::uncaught_exceptions() << std::endl;
}
}
};

int main()
{
try
{
D d;
E e;
throw 1;
}
catch(...)
{
}
}

d 的析构函数将在 1 仍然是事件异常时被调用。因此 ~d 中的 std::uncaught_exceptions() 将为 1。

对于e,其析构函数将在1 为事件异常时被调用。它的析构函数将被调用。它将构造一个D,然后再次抛出。但是由于此时 12 都没有被捕获,~d_ 中的 std::uncaught_exceptions() > 将是 2。

std::uncaught_exceptions 的全部目的就是检测这种情况。它可以让您知道对象是由于堆栈展开还是通过正常执行而被销毁。这让您知道是否应该进行不同类型的清理。考虑一个 RAII 对象,如果它由于堆栈展开而被销毁,它将回滚事务。

关于c++ - 是否可以同时有两个或多个事件异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com