throw 用于捕捉异常,将参数传递给catch,stdexcept文件中的异常类型范围很大,也可以直接传递错误值来处理。
#include<iostream>
#include<stdexcept>
#include<Windows.h>
using namespace std;
int main()
{
int a, b;
error1:try {
cin >> a >> b;
if (b == 0)
throw runtime_error("b can't be 0.\n");
}catch(runtime_error){
cout << "Do you want to input again ?\n Enter y or n." << endl;
char c;
cin >> c;
if (c == 'y')
goto error1;
}
cout << a / b;
system("pause");
return 0;
}