参考:http://c.biancheng.net/view/422.html
https://www.cnblogs.com/MrYuan/p/4800257.html
程序运行时的异常情况可以通过try...catch来“跳过”异常情况导致的程序奔溃。
#include <iostream> #include <exception> using namespace std; int main () { try { throw 1; throw "error"; } catch(char *str) { cout << str << endl; } catch(int i) { cout << i << endl; }
catch(exception e)
{
cout << e << endl;
}
cout << "end" << endl; }
try与catch组成完整的异常处理模块,try开始执行如果里面发生异常,则抛出异常并且终止。catch捕获这个异常,执行对应的异常代码。
catch捕获的异常类型可以是自定义的,也可以用标准C++异常类。
浙公网安备 33010602011771号