【C++】try 语句捕获异常,catch子句处理异常

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值