首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::exception_ptr

Defined in header <exception>

typedef /*unspecified*/ exception_ptr;

(since C++11)

std::exception_ptr是一个可空的指针类类型,它管理一个异常对象,该异常对象已被抛出并被捕获。std::current_exception.一个实例std::exception_ptr可能会传递给另一个函数,可能是在另一个线程上,其中异常可能被重新抛出并使用CATCH子句来处理。

默认构造std::exception_ptr为空指针;它不指向异常对象。

两个实例std::exception_ptr只有当它们都为NULL或在同一个异常对象上都指向相同的点时,才比较相等。

std::exception_ptr不能隐式转换为任何算术、枚举或指针类型。它在上下文上可转换为bool,如果为NULL,则计算为false,否则为true。

对象引用的异常对象。std::exception_ptr只要至少有一个仍然有效std::exception_ptr指的是:std::exception_ptr是共享所有权智能指针。

std::exception_ptr满足…的要求NullablePointer...

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
 
void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try {
        if (eptr) {
            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

二次

产出:

二次

代码语言:javascript
复制
Caught exception "basic_string::at"

二次

另见

make_exception_ptr (C++11)

creates an std::exception_ptr from an exception object (function template)

current_exception (C++11)

captures the current exception in a std::exception_ptr (function)

rethrow_exception (C++11)

throws the exception from an std::exception_ptr (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券