C++ 参考手册

位置:首页 > C++ 参考手册 >错误处理 > std::set_terminate

定义于头文件 <exception>
(C++11 前)
std::terminate_handler set_terminate( std::terminate_handler f ) noexcept;
(C++11 起)

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 ,或若未安装则为空指针值。

示例

#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)