异 常 处 理

发布时间:2006-06-28 04:35     点击:
分页:上一页  1 2 3 4 5 6 [7] 8 9 10  下一页

{

public:

virtual void f() throw (BaseEx);

virtual void g() throw (BaseEx);

virtual void h() throw (DerivedEx);

virtual void i() throw (DerivedEx);

virtual void j() throw(BaseEx);

};

class D: public A

{

public:

void f() throw (DerivedEx); //OK, DerivedEx is derived from BaseEx

class D: public A

{

public:

void f() throw (DerivedEx); //OK, DerivedEx is derived from BaseEx

void g() throw (OtherEx); //error; exception specification is

//incompatible with A's

void h() throw (DerivedEx); //OK, identical to the exception

//specification in base

void i() throw (BaseEx); //error, BaseEx is not a DerivedEx nor is it

//derived from DerivedEx

void j() throw (BaseEx,OtherEx); //error, less restrictive than the

//specification of A::j

};

};

相同的一致性限制也应用于函数指针。一个拥有exception specification函数指针只能被赋予一个有着相同或更为局限的exception specification的函数。这说明一个没有exception specification的函数指针不能被赋予一个有exception specification的函数。注意,因为exception specification不能被认为是函数类型的一部分,因此你不能声明两个仅仅是exception specification不同的函数。例如:

void f(int) throw (Y);

void f(int) throw (Z); //error; redefinition of 'void f(int)'

同样的原因,声明一个包含exception specification的typedef也是错误的:

typedef void (*PF) (int) throw(Exception); // error

在对象构造和销毁时出现异常

构造函数和析构函数被自动调用,并且它们不能够利用返回值来表明发生运行期错误。从表面上看,在对象构造和销毁时抛出一个异常似乎是报告运行期错误的最好方法。但事实上你还必须考虑一些额外的因素。你尤其应该对从析构函数中抛出异常保持警惕。

从析构函数中抛出异常是危险的
分页:上一页  1 2 3 4 5 6 [7] 8 9 10  下一页
版权申明:未经书面授权请勿转载本站信息!!作品版权归所属媒体与作者所有!!
发表评论: 匿名发表 用户名: 查看评论
您将承担一切因您的行为、言论而直接或间接导致的民事或刑事法律责任
留言板管理人员有权保留或删除其管辖留言中的任意内容
本站提醒:不要进行人身攻击。谢谢配合。
在本站搜索相关信息
2003-2005 Ksw123.com All Rights Reserved. - TOP
Copyright © 2006 Ksw123.com. All rights reserved.中国考题网 版权所有