std::set_terminate

定义于头文件 <exception>
std::terminate_handler set_terminate( std::terminate_handler f );

f 为新的全局 terminate_handler 函数并返回先前安装的 std::terminate_handler

此函数是线程安全的。每个对 std::set_terminate 的调用同步于(见 std::memory_order )后继的 std::set_terminatestd::get_terminate

(C++11 起)

目录

参数

f - 指向 std::terminate_handler 类型函数的指针,或空指针

返回值

之前安装的 terminate_handler ,或若未安装则为空指针值。

异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

示例

#include <iostream>
#include <cstdlib>
#include <exception>
 
int main()
{
    std::set_terminate([](){ std::cout << "Unhandled exception\n"; std::abort();});
    throw 1;
}

可能的输出:

Unhandled exception
bash: line 7:  7743 Aborted                 (core dumped) ./a.out

参阅

异常处理失败时调用的函数
(函数)
获得当前的 terminate_handler
(函数)
std::terminate 所调用的函数类型
(typedef)

版本历史

  • (当前 | 先前 2017年5月18日 (四) 03:20Fruderica讨论 | 贡献. . (1,389字节) (+119). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:05P12bot讨论 | 贡献 . . (1,270字节) (-106). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:12P12bot讨论 | 贡献 . . (1,376字节) (+249). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 11:37P12讨论 | 贡献 . . (1,127字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 07:00TranslationBot讨论 | 贡献. . (1,127字节) (-37). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:25P12讨论 | 贡献 . . (1,164字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 13:00TranslationBot讨论 | 贡献. . (1,164字节) (+1,164). . (Translated from the English version using Google Translate)