#include <iostream>
#include <stdexcept>
using namespace std;
int main()
{
int i1, i2;
while(cin >> i1 >> i2)
{
try
{
if (i2 == 0)
throw runtime_error("分母为0!");
cout << "i1除以i2的结果是: " << i1/i2 << endl;
}catch(runtime_error err_Sure)
{
cout << err_Sure.what() << "\n Try Again ? Enter y or n(大小写均可)" << endl;
char c;
start://乱输是没用滴,还得重头来
cin >> c;
if (!c || c == 'n'|| c == 'N')
break;
if (c == 'y' || c == 'Y')
continue;
else
{
cout << "选择不存在,请重新输入:" << endl;
goto start;
}
}
}
system("pause");
return 0;
}
【C++】try 语句捕获异常,catch子句处理异常
最新推荐文章于 2024-06-22 09:09:30 发布