异常处理之四

1,标准异常和自定义异常类
#include <iostream>
#include <stdexcept>
using namespace std;

class MyError:public logic_error
{
public:
    MyError(const string& str):logic_error(str){}
};

void func1()
{
    throw runtime_error("抛出运行异常");
}
void func2()
{
    throw logic_error("抛出逻辑异常");
}
void func3()
{
    throw MyError("抛出自定义异常");
}

int main()
{
    try{
        //func1();
        //func2();
        func3();
    }
    catch(runtime_error& err)
    {
        cout<<err.what()<<endl;
    }
    catch(MyError& err)
    {
        cout<<err.what()<<endl;
    }

    return 0;
}


2,异常规格说明

你可能感兴趣的:(异常处理)