c++中异常处理问题

代码段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的内部实现具体是怎样的哦?

阅读 2k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题