search

Home  >  Q&A  >  body text

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

迷茫迷茫2773 days ago423

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 13:53:41

    For an exception, if there is no corresponding try catch handler written, the program will call the system's predefined terminate function (terminate can be partially modified by the user), and its default operation is to terminate the program.

    reply
    0
  • Cancelreply