读书人

C++的throw

发布时间: 2012-11-09 10:18:47 作者: rapoo

C++的throw;

在github上看到一段代码,发现C++的throw;可以写在catch块外的任意位置

(一般throw;应该放在catch块中表示重新抛出异常)

所以用cygwin写了一段代码,发现可以编译成功,

而且这样抛出的异常不会被捕获任意异常的catch所捕获,

测试程序如下:

?

?

#include <iostream>using namespace std;int main(int argc, char **argv) {try {cout << "Hello, world!" << endl;throw "Oh, My God";cout << "Cannot reach!" << endl;} catch (...) {cout << "Catch exception!" << endl;}try {cout << "Hello, world!" << endl;throw;cout << "Cannot reach!" << endl;} catch (...) {cout << "Catch exception!" << endl;}}/*$ c++ -g test.cpp$ ./a.exeHello, world!Catch exception!Hello, world!terminate called without an active exceptionAborted (core dumped)*/

?我觉得这种写法更像assert(0);

?

读书人网 >C++

热点推荐