C ++中的Looser throw specifier错误 [英] Looser throw specifier error in C++

查看:2057
本文介绍了C ++中的Looser throw specifier错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码生成Looser throw specifier错误。你能帮我克服这个错误吗?

The following code is generating the "Looser throw specifier error". Could you please help me to overcome this error?

class base
{
    virtual void abc() throw (exp1);
}

void base::abc() throw (exp1)
{
    ......
}

class sub : public base
{
    void abc() throw(exp1, exp2);
}

void sub::abc() throw (exp1, exp2)
{
    .....
}


推荐答案

问题出现了,因为子类必须可用于任何基类因此不能抛出除基类中指定的异常类型之外的任何异常类型。

The problem comes about because the subclass must be usable wherever the base class can be used, and so must not throw any exception types other than those specified in the base class.

有三种解决方案:


  1. 修改基类说明符以包含任何子类可能需要抛出的每个异常类型

  2. 修改每个子类以处理每个异常类型

我建议删除它们; 他们被广泛认为是一个坏主意,部分是因为这样的问题。正如Matthieu指出的那样,标准委员会同意,并且异常说明符应在下一版本的标准中被弃用。

I would suggest removing them; they are widely regarded as a bad idea, partly because of issues like this. As Matthieu points out, the Standard Committee agrees, and exception specifiers are due to be deprecated in the next version of the Standard.

这篇关于C ++中的Looser throw specifier错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆