std::
current_exception
函数 <exception>
exception_ptr current_exception() noexcept;
|
获取指向当前异常的智能指针
返回一个exception_ptr对象,该对象指向当前处理的异常或异常的副本。
如果没有处理异常,函数将返回一个空指针值。
exception_ptr是一种共享的智能指针类型:只要至少有一个exception_ptr指向它,
被指向的异常就保证保持有效,潜在地将被指向的异常对象的生命周期扩展到其作用域之外或跨线程。
更多信息请参见exception_ptr。
这个函数不抛出异常,但是,如果函数被实现为返回指向当前处理异常副本的指针,
如果分配存储(bad_alloc)失败或复制进程失败,它可能返回一个指向不同异常的指针
(如果可能,它返回抛出的异常或bad_exception;或其他未指定的值)。
☲ 参数
-
none
☉ 返回值
指向当前处理的异常的exception_ptr对象,如果函数的内部进程会引发新的异常,则指向其他异常。
☣ 示例
// 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
⇄ 数据竞争
函数返回的对象是指向当前处理的异常,还是指向异常的副本,这取决于特定的库实现。
☂ 异常安全性
无抛出保证:该函数不抛出异常。相反,这个函数使用它的返回值来表示内部操作期间抛出的异常。
🍄 另请参阅