【问题标题】:about setting up the set_terminate function关于设置 set_terminate 功能
【发布时间】:2011-08-01 13:01:55
【问题描述】:

我正在使用 Visual Studio C++ 编译器,在学习异常处理期间,我遇到了许多 Visual C++ 编译器无法支持的功能, 比如控制可以从函数中抛出的异常。 我也无法使用 set_terminate() 修改 terminate() 的功能。 Visual C++ 修改 terminate() 是否也是一个规范?...如果是这样,那么任何人都可以解释为什么微软在其编译器中创建这些规范?...:-x

【问题讨论】:

  • 有什么问题?你不明白为什么有set_terminate() 还是什么?
  • @sharptooth:我试图使用 set_terminate() 设置终止(),但它没有这样做......我在问原因..
  • 好的,你到底做了什么,什么不工作?
  • @sharptooth:阅读下面c程序员给出的答案,我做了同样的事情,但是set_terminate无法修改terminate()的工作,你能解释一下给出的答案吗下面的cprogrammer,更清楚..
  • 原因是 Visual Studio 在调试环境中运行你的程序。这是必要的,因为您需要断点、查看数据值、访问冲突等功能。

标签: c++ visual-c++ exception-handling


【解决方案1】:

你是什么意思你无法修改终止

你试过这样的吗?

// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;

void myterminate () {
  cerr << "terminate handler called\n";
  abort();  // forces abnormal termination
}

int main (void) {
  set_terminate (myterminate);
  throw 0;  // unhandled exception: calls terminate handler
  return 0;
}

不要尝试从 VS 运行。从命令行编译和执行。

【讨论】:

  • 它将在 IDE 中工作,只是不能在调试模式下工作。 (即选择 Debug 菜单 -> Start without debugging。或按 Ctrl+F5)
  • 我使用的是 VS2008,它在发布模式下也不适用于我。是否还有其他可能的原因(例如在 Debug->Exceptions 中设置)?至少它可以在 VS2008 之外运行...
猜你喜欢
  • 2020-08-31
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多