异常的总结

(1)在匹配的过程中,不会将一种异常类型自动转换为另一个中异常。

class Exception1
{};

class Exception2
{
public:
	Exception2(const Exception1&) {}
};

void f()
{
	throw Exception1();
}

int _tmain(int argc, char* argv[])
{
	try
	{
		f();
	}
	catch(Exception2&)
	{
		cout<<"inside catch(Exception2&)"<<endl;
	}
	catch(Exception1&)
	{
		cout<<"inside catch(Exception1&)"<<endl;
	}

	return 0;
}

尽管在Exception2中有一个从Exception1到Exception2的转换构造函数,但是在匹配的过程中并不执行,而是进入匹配Exception1的异常处理器。

(2)通过基类的引用,可以捕获基类异常及其派生类的异常。

class X
{
public:
	class Trouble {};
	class Small:public Trouble {};
	class Big:public Trouble {};
	void f() {throw Big();}
};

int _tmain(int argc, char* argv[])
{
	X x;
	try
	{
		x.f();
	}
	catch(X::Trouble&)
	{
		cout<<"catch Trouble"<<endl;
	}
	catch(X::Small&)
	{
		cout<<"catch Small"<<endl;
	}
	catch(X::Big&)
	{
		cout<<"catch Big"<<endl;
	}

	return 0;
}

以上输出catch Trouble。

通常的做法:首先捕获派生类的异常,并且将基类放到最后用于捕获其他不太具体的异常。

(3)函数级try块。


 

class Base
{
public:
	class BaseExcept {};
	Base(int ii):i(ii) {throw BaseExcept();}
private:
	int i;
};

class Derived:public Base
{
public:
	class DerivedExcept 
	{
		const char* msg;
	public:
		DerivedExcept(const char* msg):msg(msg) {}
		const char* what() const {return msg;}
	};

	Derived(int j)try:Base(j)
	{
		cout<<"This won't print"<<endl;
	}
	catch(BaseExcept&)
	{
		throw DerivedExcept("Base subobject threw");
	}
};

int _tmain(int argc, char* argv[])
{
	try
	{
		Derived d(3);
	}
	catch(Derived::DerivedExcept& d)
	{
		cout<<d.what()<<endl;
	}
	return 0;
}

上述派生类的构造函数就用了函数级的try块,这样的用法可以捕捉基类或者成员对象在构造过程中抛出的异常。构造函数的函数体就是try块,异常处理器在函数体外。
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值