首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++的throw

2012-11-09 
C++的throw在github上看到一段代码,发现C++的throw可以写在catch块外的任意位置(一般throw应该放在catc

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);

?

热点排行