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

std::rethrow_exception

Defined in header <exception>

[noreturn] void rethrow_exception( std::exception_ptr p )

(since C++11)

抛出以前捕获的异常对象(由异常指针引用)。p...

参数

p

-

non-null std::exception_ptr

返回值

%280%29

二次

代码语言: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"

二次

另见

exception_ptr (C++11)

shared pointer type for handling exception objects (typedef)

current_exception (C++11)

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券