异常处理是按照抛出表达式顺序进行的

#include <iostream>
using namespace std;
int main()
{
	int i;
	char ch;
	cout << "Please enter an integer and a character:\n";
	try
	{
		cin >> i >> ch;
		if (i == 0)throw 0;
		if (ch == '!')throw '!';
	}
	catch (int)
	{
		cout << "'0' is an illegal integer." << endl;
	}
	catch (char)
	{
		cout << "'!' is an illegal character." << endl;
	}
	cout << "The end of the programming.";
}

结果分析:
①输入非零数,非!字符:The end of the programming.
②输入非零数,!:’!’ is an illegal character.
③输入0,任意字符:‘0’ is an illegal integer.

需注意:若输入0 !,并不会依次输出’!’ is an illegal character.”和“‘0’ is an illegal integer.两句,只会输出’0’ is an illegal integer.因为这是按照抛出表达式在此程序中的顺序进行的。一旦控制转移给句柄(即catch语句块),就不会自动返回。因此,即使输入0 !,也不会执行throw’!’,从而不会输出’!’ is an illegal character.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值