c++中异常处理问题

浏览:67日期:2023-04-30

问题描述

代码段1

#include <iostream>#include <cstring> #include <string> #include <typeinfo>using namespace std;int main() {string str = 'ss';try{ throw 'hello';}catch (const char* e){ cout << e << endl;}cout << str << endl; return 0; }

代码段2

#include <iostream>#include <cstring>#include <string>#include <typeinfo>using namespace std;int main(){ string str = 'ss'; //try //{throw 'hello'; //} //catch (const char* e) //{ // cout << e << endl; //} cout << str << endl; return 0;}

程序代码如上所示,请问为什么当throw之后没有被catch,程序会直接终止哦?而当catch到之后,程序之后可以继续执行后面的代码?throw的内部实现具体是怎样的哦?

问题解答

回答1:

对于一个异常,如果没有书写对应的try catch处理,那么程序将调用系统预定义的 terminate 函数(terminate 可以被用户部分修改),其默认操作为中止程序。

相关文章: