例外和继承 [英] Exceptions and inheritance

查看:93
本文介绍了例外和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于

异常的继承是如何工作的我有点困惑。


class ParentEx:public std :: exception

{

};


类SubEx:public ParentEx,std :: runtime_error

{

};


SubEx被捕获ParentEx或SubEx而被捕获,但不是

std :: exception。

为什么?


谢谢。

解决方案

Noah Roberts写道:

我对继承如何在
例外显然有点困惑。

类ParentEx:public std :: exception
{
};

类SubEx:public ParentEx,std :: runtime_error
{
};

SubEx被抓住了捕捉ParentEx或SubEx,但不是
std :: exception。

为什么?




因为std :: exception是不明确的基础。有两个。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


< blockquote>

Pete Becker写道:

因为std :: exception是一个含糊不清的基础。有两个。




标题中是否有两个或因为我使用多重继承

并且父母都来自该类?




Noah Roberts写道:

Pete Becker写道:

因为std :: exception是一个模糊的基础。有两个。
标题中是否有两个或因为我使用多个



继承并且父母都从该类派生?



这是因为多重继承。


希望这会有所帮助,

-shez-


I am a bit confused about how inheritance works with regard to
exceptions apparently.

class ParentEx : public std::exception
{
};

class SubEx : public ParentEx,std::runtime_error
{
};

SubEx is getting caught by catching ParentEx or SubEx, but not
std::exception.

Why?

Thanks.

解决方案

Noah Roberts wrote:

I am a bit confused about how inheritance works with regard to
exceptions apparently.

class ParentEx : public std::exception
{
};

class SubEx : public ParentEx,std::runtime_error
{
};

SubEx is getting caught by catching ParentEx or SubEx, but not
std::exception.

Why?



Because std::exception is an ambiguous base. There are two of them.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Pete Becker wrote:

Because std::exception is an ambiguous base. There are two of them.



Is there two in the headers or because I am using multiple inheritance
and both parents derive from that class?



Noah Roberts wrote:

Pete Becker wrote:

Because std::exception is an ambiguous base. There are two of them.
Is there two in the headers or because I am using multiple


inheritance and both parents derive from that class?


It is because of the multiple inheritance.

Hope this helps,
-shez-


这篇关于例外和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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