C++异常处理

一、异常处理

在c++中,异常处理使用throw抛出异常,try…catch执行异常处理内容。
例如,在进行除法运算时,除数不能为0,我们可以采用返回一个标识数字判断计算异常,如-1,0等,但这样就会存在与某些结果产生歧义,比如运算结果就-1,或者0.这还只是简单的情况,对于复杂的程序,其异常有很多种,那么,异常处理则是必然需要的。
设计异常抛出时,throw可能出现异常的数据,编写相应异常提示语句

#include<iostream>
#include<string>
using namespace std;
template<class T>
T mydiv(T a, T b)
{
	if (!b)
		throw b;

	return a / b;
}
template<class T>
void test()
{
	T a, b;
	cin >> a >> b;
	try {
		cout << "res=" << mydiv<T>(10.0, 0) << endl;
	}
	catch (int e) {
		cout << "int类型异常 " << e << endl;
	}
	catch (float e) {
		cout << "float类型异常 " << e << endl;
	}
	catch (double e) {
		cout << "double类型异常 " << e << endl;
	}
}
int main()
{
	cout << "请输入两int类型变量:" << endl;
	test<int>();
	cout << "请输入两float类型变量:" << endl;
	test<float>();
	cout << "请输入两double类型变量:" << endl;
	test<double>();
	return 0}

如此,便将程序中所存在的异常清晰的指了出来,且不影响后续程序执行。

二、栈解旋

#include<iostream>
#include<string>
using namespace std;
class Person
{
	string name;
public:
	Person(string name) :name(name) { cout <<name<<"的构造函数" << endl; }
	~Person(){cout<<name<<"的析构函数"<<endl; }
};
int main()
{
	try {
		Person p1("hhh");
		Person p2("aaa");
		Person p3("eee");
		throw 10;
	}
	catch (int)
	{
		cout << "int类型异常" << endl;
	}
	return 0;
}

从try块起至异常被抛出前,期间创建在栈上的对象会自动析构且与构造顺序相反,因此称其为栈解旋。

三、异常规范(C++11已弃用)

#include<iostream>
#include<string>
using namespace std;
void Throwtest() throw()
{
	//throw "hh";
	throw 123;
}
int main()
{
	try {
		Throwtest();
	}
	catch(int e)
	{
		cout << "int类型异常 "<<e << endl;
	}
	catch (string e)
	{
		cout << "string类型异常" << e << endl;
	}
	catch (char e)
	{
		cout << "char类型异常" << e << endl;
	}
}

四、异常的生命周期

#include<iostream>
#include<string>
using namespace std;
class MyException
{
public:
	MyException() { cout << "异常的构造函数" << endl; }
	~MyException() { cout << "异常的析构函数" << endl; }
};
int main()
{
	try {
		throw new MyException;
	}
	catch(MyException e)
	{
		cout << "捕获到MyExpection异常 "<<endl;
	}
	catch (MyException&e)
	{
		cout << "捕获到MyExpection&异常 " << endl;
	}
	catch (MyException*e)
	{
		cout << "捕获到MyException*异常" << endl;
		delete e;//注意释放
	}
}

五、 异常的多态使用

#include<iostream>
using namespace std;
class BaseException {
public:
	virtual void printError() {};
};
class NULLPointerError:public BaseException
{
public:
	virtual void printError() {
		cout << "空指针异常!" << endl;
	}
};
int main()
{
	try {
		throw NULLPointerError();
	}
	catch (BaseException&ex) {
		ex.printError();
	}
}

六、cin的部分使用

#include<iostream>
using namespace std;
int main()
{
	char res;
	res=cin.get();
	cout << res<<endl;
	cin.putback(res);
	cout << "偷窥缓冲区的数据" << cin.peek() << endl;
	cout<<"忽略两字节偷窥 "<<cin.ignore(2).peek()<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值