C++中的异常

异常:预料到会发生,但是不一定会发生,程序有返回值,用来检验是否运行正确

异常机制:

Exception:最大的好处就是有清晰的错误处理代码,把业务逻辑错误处理分开了

在堆里面:有且只有new出来的东西

在全局数据区里:全局变量、静态本地变量、静态成员变量

在堆栈里:本地变量

异常:就是由于外部条件的原因,有可能会抛出错误

三种try和catch的形式:

void outer(){
    try{
        func();
        func2();
    }catch(VectorIndexError& e){
        e.diagnostic();
    }
    cout<<"Control is here after exception!"<<endl;
}

void outer2(){
    String err("exception caught");
     try{
         func();
     }catch(VectorIndexError){
         cout<<err<<endl;
         throw;
     }
}

void outer3(){
    try{
        outer2();
    }catch(...){
        cout<<"The exception stops here!"<<endl;
    }
}

 

 

猜你喜欢

转载自blog.csdn.net/hello_leiyuanyi/article/details/80587770
今日推荐