C++ try catch的问题

来源:互联网 发布:一键获取淘宝联盟 编辑:程序博客网 时间:2024/05/19 23:53

问题

之前一直以为C++的try、catch和C#一样,程序出现异常的话,会自己抛出,但是今天试了一下,发现是需要自己判断是否有异常,如果有异常的话,需要手动throw异常。

代码

#include<iostream>#include<exception>using namespace std;int main(){    cout << "start!" << endl;    int a = 1;    int b = 0;    try    {        //int c = a / b;        throw 2;    }    catch (...)    {        cout << "exception!" << endl;    }    cout << "end!" << endl;    system("pause");    return EXIT_SUCCESS;}如果取消两个数相除的那一行的注释,则会直接报异常,而不会进入`catch`。
0 0
原创粉丝点击