【发布时间】:2013-05-24 03:12:19
【问题描述】:
这段代码:
#include <iostream>
#include <stdexcept>
using namespace std;
int throw_it() {
throw range_error( "foo" );
}
int main() {
try {
throw throw_it();
}
catch ( exception const &e ) {
cerr << e.what() << endl;
return 0;
}
}
在运行时打印foo,但是否保证会这样做?更具体地说,在抛出异常的过程中抛出异常会导致定义的行为吗?这种行为是否会引发最近抛出的异常(就像上面的测试代码一样)?
仅供参考:
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
【问题讨论】:
-
我现在太累了,但是 this here 似乎是一篇关于该主题的有趣文章。
-
@StefanFalk Windows SEH 和 C++ 异常是两个不同的东西。
-
您不是“正在抛出异常”。你正在调用一个函数并计划使用它的返回值来抛出,但是当函数实际上没有返回时,你的计划就毁了。