std::
rethrow_exception
函数 <exception>
noreturn void rethrow_exception (exception_ptr p);
|
重新抛出异常
抛出p指向的异常对象。
☲ 参数
-
p
-
指向异常对象的exception_ptr对象。
此参数不能为空exception_ptr。
Exception_ptr是一个指向异常的类指针类型。
☉ 返回值
None(函数永远不会返回)。
☣ 示例
// exception_ptr example
#include <iostream> // std::cout
#include <exception> // std::exception_ptr, std::current_exception, std::rethrow_exception
#include <stdexcept> // std::logic_error
int main () {
std::exception_ptr p;
try {
throw std::logic_error("some logic_error exception"); // throws
} catch(const std::exception& e) {
p = std::current_exception();
std::cout << "exception caught, but continuing...\n";
}
std::cout << "(after exception)\n";
try {
std::rethrow_exception (p);
} catch (const std::exception& e) {
std::cout << "exception caught: " << e.what() << '\n';
}
return 0;
}
|
输出:
exception caught, but continuing...
(after exception)
exception caught: some logic_error exception
⇄ 数据竞争
对引用相同异常的exception_ptr对象并发调用rethrow_exception是安全的。
请注意,有些实现在进入catch异常处理块时可能不会执行指向对象的副本,
在这种情况下,并发访问重新抛出的异常对象可能会引入数据竞争。
☂ 异常安全性
抛出一个异常。
如果p为空exception_ptr,则会导致未定义的行为。
🍄 另请参阅