c++ - 设置终止和意外处理程序

标签 c++ exception

你能解释一下吗:

terminate_handler set_terminate (terminate_handler f) throw();

还有这个:

unexpected_handler set_unexpected (unexpected_handler f) throw();

要更改我们使用的终止处理程序,必须使用 set_terminate(),如上所示,但我无法理解/解释上面的形式。谁能解释一下。

我也很难理解这一点:

terminate_handler set_terminate (terminate_handler f) throw();

Here, f is a pointer to the new terminate handler.The function returns a pointer to the old terminate handler. The new terminate handler must be of type terminate_handler, which is defined like this:

typedef void(*terminate_handler)();

最佳答案

terminate_handler 是函数指针的类型定义。当您设置终止处理程序时,您将传递一个指向要在终止时调用的函数的指针。这是 set_terminate 的参数。该函数返回旧指针。这样,如果您只想在短时间内使用自己的终止处理程序,则可以在完成后恢复前一个:

void my_terminator() {
    // whatever
}

int main() {
    // terminate here calls default handler

    terminate_handler old_handler = set_terminate(my_terminator);
    // now, terminate will call `my_terminator`

    set_terminate(old_handler);
    // now, terminate will call the default handler

    return 0;
}

关于c++ - 设置终止和意外处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47110343/

相关文章:

c++ - 为什么单一虚拟继承不足以解决可怕的菱形继承(钻石问题)?

用于删除和重置指针的 C++ 模板函数

javascript - HtmlUnit - 无法调用未定义的方法 "replace"

c++ - 如何将不可复制的 std::function 存储到容器中?

c++ - 链接器:如果我在程序中不使用 float ,则 "__fltused"被多重定义

c++ - Crypto++ pbkdf2 输出不同于 Rfc2898DeriveBytes (C#) 和 crypto.pbkdf2 (JavaScript)

python - 测试自定义异常的引发时出错(使用 assertRaises())

java - 为什么 IllegalArgumentException (JDK 1.4.2) 不能用可抛出的原因构造?

c# - 没有成员访问时怎么会出现NullReferenceException呢?

c# - 如何取消在 C# Winforms 应用程序中执行长时间运行的异步任务