std::rethrow_exception

定义于头文件 <exception>
[[noreturn]] void rethrow_exception( std::exception_ptr p )
(C++11 起)

抛出先前捕获的异常对象,它为异常指针 p 所引用。

目录

参数

p - 非空 std::exception_ptr

返回值

(无)

示例

#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
 
void handle_eptr(std::exception_ptr eptr) // 按值传递 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); // 这生成一个 std::out_of_range
    } catch(...) {
        eptr = std::current_exception(); // 捕获
    }
    handle_eptr(eptr);
} // std::out_of_range 的析构函数调用于此,在 ept 析构时

输出:

Caught exception "basic_string::at"

参阅

处理异常对象的共享指针类型
(typedef)
std::exception_ptr 中捕获当前异常
(函数)

版本历史

  • (当前 | 先前 2017年5月17日 (三) 09:40Fruderica讨论 | 贡献. . (577字节) (-435). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:45P12bot讨论 | 贡献 . . (1,012字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:04P12bot讨论 | 贡献 . . (1,012字节) (-64). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:20P12bot讨论 | 贡献 . . (1,076字节) (+281). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 11:37P12讨论 | 贡献 . . (795字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 07:00TranslationBot讨论 | 贡献. . (795字节) (-41). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:25P12讨论 | 贡献 . . (836字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 13:00TranslationBot讨论 | 贡献. . (836字节) (+836). . (Translated from the English version using Google Translate)