捕获异常时需要针对异常的类型:
#include <iostream>
using namespace std;
int main()
{
int tmp;
while (cout<<"Please enter 1, 2 or 3"<<endl && cin >> tmp)
{
try{
if (tmp == 1)
throw (1);
if (tmp == 2)
throw (1.0);
if (tmp == 3)
throw ("string");
break;
}
catch (const int &i){
cout << "it's an interger."<<endl;
continue;
}
catch (const double &i){
cout << "it's an double."<<endl;
continue;
}
catch (const char* &s){
cout << "it's an string."<<endl;
continue;
}
catch (...)
{
cout << "You can never see this."<<endl;
continue;
}
}
return 0;
}