【发布时间】:2011-11-05 22:00:24
【问题描述】:
考虑下面的代码:
#include <iostream>
#include <exception>
void third_party_function() throw () {
throw -1; // oops
}
void recover() throw (std::exception) {
std::cout << "We will throw std::exception() to avoid terminate() to be called.\n";
throw std::exception();
}
int main(int argc, char** argv) {
try {
std::set_unexpected(recover);
third_party_function();
} catch (std::exception e) {
std::cout << "Unexpected exception: " << e.what() << '\n';
}
return 0;
}
这是程序的输出:
我们将抛出 std::exception() 以避免调用 terminate()。 在抛出 'std::exception' 实例后调用终止
什么():标准::异常
我不明白为什么在任何情况下都会调用 terminate()(因此永远不会捕获 std::exception),尽管事实上我做了 Stroustrup 建议的类似示例以避免调用 terminate()在调用处理程序之后(参见 C++ 编程语言,第 3 版,第 14.6 章)
【问题讨论】:
-
蛋糕是个谎言。不记录您正在使用的 CRT 没有多大意义。
-
CRT 代表什么?
-
C 运行时。记录您正在使用的编译器和操作系统。
-
Linux 2.6.31-22-generic-pae #73-Ubuntu SMP Fri Feb 11 18:39:01 UTC 2011 i686 GNU/Linux gcc 版本 4.4.1 使用内置规范。目标:i486-linux-gnu 线程模型:posix