std::uncaught_exception
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <exception> 中定义
|
||
bool uncaught_exception(); |
||
检测如果当前线程有一个活的异常对象,也就是说,一个异常被抛出,尚未进入了一个匹配的catch子句,std::terminate或std::unexpected。换句话说,std::uncaught_exception检测,如果堆栈展开正在进行中.....
原文:
Detects if the current thread has a live exception object, that is, an exception has been thrown and not yet entered a matching catch clause, std::terminate or std::unexpected. In other words, std::uncaught_exception detects if stack unwinding is currently in progress.
有时它是安全的,抛出一个异常,甚至在std::uncaught_exception() == true。例如,如果已捕获的异常在析构函数中被忽略,他们不能传播,它不会导致std::terminate.
原文:
Sometimes it's safe to throw an exception even while std::uncaught_exception() == true. For example, if exceptions are caught and ignored in a destructor, they can't propagate out of it and won't lead to std::terminate.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
true是目前正在进行中,在这个线程堆栈展开.
原文:
true if stack unwinding is currently in progress in this thread.
[编辑] 例外
[编辑] 示例
#include <iostream> #include <exception> #include <stdexcept> struct Foo { ~Foo() { if (std::uncaught_exception()) { std::cout << "~Foo() called during stack unwinding\n"; } else { std::cout << "~Foo() called normally\n"; } } }; int main() { Foo f; try { Foo f; std::cout << "Exception thrown\n"; throw std::runtime_error("test exception"); } catch (const std::exception& e) { std::cout << "Exception caught: " << e.what() << '\n'; } }
输出:
Exception thrown ~Foo() called during stack unwinding Exception caught: test exception ~Foo() called normally
[编辑] 另请参阅
异常处理函数调用时失败 原文: function called when exception handling fails (函数) | |
(C++11) |
处理异常对象的共享指针类型 原文: shared pointer type for handling exception objects (typedef) |