std::current_exception
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <exception> 中定义
|
||
std::exception_ptr current_exception() |
(C++11 起) | |
如果在异常处理过程中(通常在一个catch条),抓住了当前的异常对象,并创建一个std::exception_ptr持有该异常对象的引用,或该异常对象的副本(这是实现定义的副本是制)
原文:
If called during exception handling (typically, in a catch clause), captures the current exception object and creates an std::exception_ptr that holds a reference to that exception object, or to a copy of that exception object (it is implementation-defined if a copy is made)
如果实现这个功能需要调用new和调用失败,返回的指针将举行std::bad_alloc的一个实例
原文:
If the implementation of this function requires a call to new and the call fails, the returned pointer will hold a reference to an instance of std::bad_alloc
如果此功能的实现需要复制捕获的异常对象和它的拷贝构造函数抛出一个异常,返回的指针将举行一个抛出的异常。如果还抛出,抛出的异常对象的拷贝构造函数返回的指针可能会持有一个引用的一个实例std::bad_exception打破的死循环.....
原文:
If the implementation of this function requires to copy the captured exception object and its copy constructor throws an exception, the returned pointer will hold a reference to the exception thrown. If the copy constructor of the thrown exception object also throws, the returned pointer may hold a reference to an instance of std::bad_exception to break the endless loop.
如果函数被调用时也不例外处理,则返回一个空的std::exception_ptr.
原文:
If the function is called when no exception is being handled, an empty std::exception_ptr is returned.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
std::exception_ptr一个参考的异常对象,或一个异常对象的副本,或实例std::bad_allocstd::bad_exception的一个实例的实例.
原文:
An instance of std::exception_ptr holding a reference to the exception object, or a copy of the exception object, or to an instance of std::bad_alloc or to an instance of std::bad_exception.
[编辑] 例外
[编辑] 示例
#include <iostream> #include <string> #include <exception> #include <stdexcept> void handle_eptr(std::exception_ptr eptr) // passing by value is ok { try { if (eptr != std::exception_ptr()) { std::rethrow_exception(eptr); } } catch(const std::exception& e) { std::cout << "Caught exception \"" << e.what() << "\"\n"; } } int main() { std::exception_ptr eptr; try { std::string().at(1); // this generates an std::out_of_range } catch(...) { eptr = std::current_exception(); // capture } handle_eptr(eptr); } // destructor for std::out_of_range called here, when the eptr is destructed
输出:
Caught exception "basic_string::at"
[编辑] 另请参阅
(C++11) |
处理异常对象的共享指针类型 原文: shared pointer type for handling exception objects (typedef) |
(C++11) |
从std::exception_ptr抛出的异常 原文: throws the exception from an std::exception_ptr (函数) |
(C++11) |
创建一个异常对象的std::exception_ptr 原文: creates an std::exception_ptr from an exception object (函数模板) |