c++中异常处理问题
迷茫
迷茫 2017-04-17 13:51:41
[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的内部实现具体是怎样的哦?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
ringa_lee

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号