c++学习笔记--异常处理

来源:互联网 发布:生死手游刷金币软件 编辑:程序博客网 时间:2024/06/05 04:53
代码1.
int main(int argc, const char *argv[]){        try    {        cout << "foo" << endl;        throw std::runtime_error("exception ...");        cout << "bar" << endl;    }    catch(...)    {        cout << "In catch .." << endl;     }    cout << "continue ..." << endl;        return 0;}

这段代码的输出结果:

foo

In catch

continue...

程序在运行try时,执行到 throw语句 ,抛出异常去执行 catch内的内容,而throw下面的 cout<<"bar"<<endl;不会执行 

2.

int main(int argc, const char *argv[]){    int a, b;    try    {        cin >> a >> b;        if(b == 0)            throw runtime_error("除数不能为零!");        int res = a / b;        cout << "res = " << res << endl;    }    catch(exception &e)    {        cout << "异常信息: " << e.what() << endl;    }    return 0;}

如果输入的b的值为0 则抛出异常 ,执行catch语句 里面的内容 “异常信息:除数不能为零!”。如果b不是0则执行 throw下面的语句。

3.

int main(int argc, const char *argv[]){    cout << "Before ..." << endl;    //throw exception();    throw runtime_error("出现异常");    cout << "After .." << endl;    return 0;}

这段代码编译不会通过,因为当执行 throw语句时 ,没有catch语句来处理异常。

4.

//对异常的捕获处理int main(int argc, const char *argv[]){    try    {        throw runtime_error("出现运行期错误");    }    catch(exception &e)    {        cout << "异常信息:" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}

5.

//对于不同的异常可以采取不同的catch块进行捕捉int main(int argc, const char *argv[]){    try    {        int i;        cin >> i;        if(i == 0)            throw runtime_error("出现运行期错误");        else if(i == 1)            throw invalid_argument("非法参数");    }    catch(runtime_error &e)    {        cout << "runtime_error :" << e.what() << endl;    }    catch(invalid_argument &e)    {        cout << "invalid_argument:" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}
6.

//异常可以统一处理int main(int argc, const char *argv[]){    try    {        int i;        cin >> i;        if(i == 0)            throw runtime_error("出现运行期错误");        else if(i == 1)            throw invalid_argument("非法参数");    }    catch(exception &e)    {        cout << "exception :" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}

7.

//异常捕获不到,照样因为core dumpint main(int argc, const char *argv[]){    try    {        int i;        cin >> i;        if(i == 0)            throw runtime_error("出现运行期错误");        else if(i == 1)            throw invalid_argument("非法参数");    }    catch(runtime_error &e)    {        cout << "runtime_error :" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}

8.

//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){    try    {        int i;        cin >> i;        if(i == 0)            throw runtime_error("出现运行期错误");        else if(i == 1)            throw invalid_argument("非法参数");        else if(i == 2)            throw logic_error("逻辑错误");        else            throw out_of_range("越界错误");    }    catch(runtime_error &e)    {        cout << "runtime_error :" << e.what() << endl;    }    catch(invalid_argument &e)    {        cout << "invalid_argument:" << e.what() << endl;    }    catch(exception &e)    {        cout << "异常信息:" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}

9.

//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){    try    {        int i;        cin >> i;        if(i == 0)            throw runtime_error("出现运行期错误");        else if(i == 1)            throw invalid_argument("非法参数");        else if(i == 2)            throw logic_error("逻辑错误");        else            throw out_of_range("越界错误");    }    catch(...) //能捕获所有的异常    {        }    catch(exception &e)    {        cout << "异常信息:" << e.what() << endl;    }    catch(runtime_error &e)    {        cout << "runtime_error :" << e.what() << endl;    }    catch(invalid_argument &e)    {        cout << "invalid_argument:" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}
这段代码会出现运行错误,因为 一开始就能捕获全部异常,那么对下面catch就失效了
10.

void test(){    int i;    cin >> i;    if(i == 0)        throw runtime_error("出现运行期错误");    else if(i == 1)        throw invalid_argument("非法参数");    else if(i == 2)        throw logic_error("逻辑错误");    else        throw out_of_range("越界错误");}//对于不同的异常可以采取不同的catch块进行捕捉//对于一部分可以统一处理int main(int argc, const char *argv[]){    try    {        test();    }    catch(runtime_error &e)    {        cout << "runtime_error :" << e.what() << endl;    }    catch(invalid_argument &e)    {        cout << "invalid_argument:" << e.what() << endl;    }    catch(exception &e)    {        cout << "异常信息:" << e.what() << endl;    }    cout << "继续运行" << endl;    return 0;}

catch(...) 与 catch(exception &e) 的区别在于 前者不知道异常的类型,后者可以做到

0 0