C++之try和catch入门

来源:互联网 发布:手机淘宝销量排名 编辑:程序博客网 时间:2024/05/18 00:34
//流程://try---->throw  -----> catch()//打印2222222!//流程Div->thow b->catch(int)#include <iostream>  using namespace std;  int Div(int a, int b)  {      if(b == 0)      {          throw b;      }      cout<<"OK"<<endl;      return a/b;  }  void main()  {  int a,b;  a = 1;    b = 0;    try  {          Div(a,b);    }      catch(int)      {          cout<<"2222222!"<<endl;      }      catch(...)      {          cout<<"11111111110!"<<endl;      } }  //打印1111111111!//流程Div->catch(...)  #include <iostream>  using namespace std;  int Div(int a, int b)  {      cout<<"OK"<<endl;      return a/b;  }  void main()  {      int a,b;      a = 1;b = 0;    try      {          Div(a,b);    }      catch(...)      {          cout<<"2222222!"<<endl;      }     catch(...)      {          cout<<"1111111111!"<<endl;      } }  
0 0
原创粉丝点击