异常处理 try catch

#include <iostream>
using namespace std;

void func(int a)
{
   if(a == 0)
   {
      throw string("a is error");
      throw a;
   }
}

int main()
{
   try
   {
      func(0);
   }
   catch(int a)
   {
      cout << "a " << a << endl;
   }
   catch(const string &s)
   {
      cout << s << endl;
   }

   return 0;
}
$ ./a.out    
a is error
posted @ 2022-08-19 08:41  thomas_blog  阅读(19)  评论(0)    收藏  举报