分页:
上一页 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 下一页