c**********e 发帖数: 2007 | 1 #include
class A {
public:
~A() { throw "error"; }
};
void foo() { A a; throw "error"; }
int main()
{
try { foo(); }
catch (const char*) { return 1; }
return 0;
}
What is the result of executing the sample above?
a) The program exits normally with a result code of 0.
b) The program exits normally with a result code of 1.
c) unexpected() is invoked.
d) terminate() is invoked.
e) There is an uncaught exception because "error" is thrown twice and only
caught once. |
z****e 发帖数: 2024 | |
c**********e 发帖数: 2007 | 3 Not the answer given.
【在 z****e 的大作中提到】 : d
|
z****e 发帖数: 2024 | 4 then your answer is wrong.
【在 c**********e 的大作中提到】 : Not the answer given.
|
c**********e 发帖数: 2007 | 5 You do not understand what really happens.
【在 z****e 的大作中提到】 : then your answer is wrong.
|
z****e 发帖数: 2024 | 6 LOL
【在 c**********e 的大作中提到】 : You do not understand what really happens.
|
z****e 发帖数: 2024 | 7 try the following to see what you are.
#include
#include
void SB_question () {
std::cout<<"i'm an SB"<
}
class A {
public:
~A() { throw "error"; }
};
void foo() { A a; throw "error"; }
int main()
{
std::set_terminate (SB_question);
try { foo(); }
catch (const char*) { return 1; }
return 0;
}
【在 c**********e 的大作中提到】 : You do not understand what really happens.
|
r****t 发帖数: 10904 | |
t****t 发帖数: 6806 | 9 actually i think you do not understand. the "given" answer is from you, or
from the author? if it is from you, i would say you need to improve your c++
knowledge and please be careful not to mislead other readers. if it is from
the author, apparently the questions are outdated or the author himself
need to improve his c++ knowledge.
【在 c**********e 的大作中提到】 : You do not understand what really happens.
|
t****t 发帖数: 6806 | 10 actually not to throw from destructor is the usual practice. see effective c
++.
【在 r****t 的大作中提到】 : 太夸张了,这是人写的程序么。
|
l******e 发帖数: 12192 | 11 faq lite也有
讲exception基本都要讲
c
【在 t****t 的大作中提到】 : actually not to throw from destructor is the usual practice. see effective c : ++.
|
c**********e 发帖数: 2007 | 12 I apologize to zaoxie. I misread your answer. Your answer is correct.
Thank thrust for the valuable comment.
d) terminate() is invoked.
This is the correct choice. It is a serious programming error to throw from
a destructor if an exception is already causing stack unwinding (C++
Standard 15.2/3). Throwing from a constructor is usually discouraged, but
if it is to be done, then uncaught_exception() should be checked first (C++
Standard 15.5.3/1).
【在 z****e 的大作中提到】 : then your answer is wrong.
|
c**********e 发帖数: 2007 | 13 hehe...This is the output:
i'm an SB
Segmentation fault
【在 z****e 的大作中提到】 : try the following to see what you are. : #include : #include : void SB_question () { : std::cout<<"i'm an SB"<: } : class A { : public: : ~A() { throw "error"; } : };
|