avatar
C++ Q39: throw new (C1)# JobHunting - 待字闺中
c*e
1
class A {};
class B : A {};
void foo() { throw new B; }
Which of the following catch statements successfully catches the exception
foo throws?
a) catch (A* e) and catch (B* e)
b) catch (A e) and catch (B e)
c) catch (A* e)
d) catch (B* e)
e) catch (A e)
I have no answer. Help, please.
avatar
s*t
2
why a is not right?
avatar
s*t
3
异常处理我一点不懂。
刚才试了一下,如果throw new A也不能catch B*
avatar
s*t
4
which is subtype and super type?
I think you might be wrong. Consider the following change, what is your
answer?
class A {};
class B : A {};
void foo() { throw new A; }
Which of the following catch statements successfully catches the exception
foo throws?
a) catch (A* e) and catch (B* e)
b) catch (A e) and catch (B e)
c) catch (A* e)
d) catch (B* e)
e) catch (A e)
avatar
M*q
5
that's exactly why A* can catch new B(), i think
avatar
c*e
6
I think it is d). You throw a pointer to B, then you need to catch it. Here
to catch a pointer to A is not enough.
avatar
s*t
7
this makes perfect sense

Here

【在 c**********e 的大作中提到】
: I think it is d). You throw a pointer to B, then you need to catch it. Here
: to catch a pointer to A is not enough.

avatar
x*y
8
Sorry, you are right...A* can only be caught by A* but B* can be caught by A
* and B*...

【在 s*****t 的大作中提到】
: which is subtype and super type?
: I think you might be wrong. Consider the following change, what is your
: answer?
: class A {};
: class B : A {};
: void foo() { throw new A; }
: Which of the following catch statements successfully catches the exception
: foo throws?
: a) catch (A* e) and catch (B* e)
: b) catch (A e) and catch (B e)

avatar
x*y
9
Sorry, I made a mistacke, it should be a.

【在 s*********t 的大作中提到】
: why a is not right?
avatar
s*t
10
it's not a.
u just got the right answer but ur explaination wasn't

【在 x***y 的大作中提到】
: Sorry, I made a mistacke, it should be a.
avatar
x*y
11
Sorry, I made a mistake...A* can only be caught by A*, B* can be caught by A
* and B* as the thrown B* is also A* object....

【在 s*********t 的大作中提到】
: 异常处理我一点不懂。
: 刚才试了一下,如果throw new A也不能catch B*

avatar
x*y
12
It shoudl be a, my original answer is wrong....All the public inheritence
relations in c++ satisfy that the derived object is also a base object. here
, a B* is thrown, which is also a A*, that is why it can caught by both A*
and B*.
I verified in my programme and the throw new B can be caught by catch(A*).

【在 s*********t 的大作中提到】
: it's not a.
: u just got the right answer but ur explaination wasn't

avatar
x*y
13
Sorry, I made a mistacke, you are right....

【在 M******q 的大作中提到】
: that's exactly why A* can catch new B(), i think
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。