-
January 12th, 2009, 04:07 AM
#1
exception in the destructor
Which of the following reasons describe why a destructor cannot throw an exception in C++?
A. The C++ language does not permit it; a throw statement in a destructor will be caught as an error by the compiler.
B. It can invoke the terminate() handler.
C. Since the object is being destroyed, it is illogical to throw an exception then.
D. A destructor may be invoked as a result of stack unwinding while an exception is being handled.
E. A destructor in C++ cannot implement a try...catch block.
Ok,
* A is incorrect - you can throw an exception in an destructor
* B is correct - since if a destructor, called by the language runtime during stack unwinding, terminates
with an exception the whole program is terminated.
* C is correct - not sure why, but that answer makes sense to me.
* D unsure
* E is incorrect.
Can someone please confirm/comment? Thx
"What comes around, goes around"
-
January 12th, 2009, 04:12 AM
#2
Re: exception in the destructor
I have read pretty much of your multiple choices lately, is there an urge to ask for a resolution. My second post would help to withraw the stuck internal exception problems. Yours sounds like homework assignments that guards of companies need to solve during a few hours.
-
January 12th, 2009, 04:17 AM
#3
Re: exception in the destructor
They are from a online quiz I'm working through to help me understand the finer details of c++.
"What comes around, goes around"
-
January 12th, 2009, 04:19 AM
#4
Re: exception in the destructor
Where is the url ? I 'd like to learn too
-
January 12th, 2009, 04:24 AM
#5
Re: exception in the destructor
"What comes around, goes around"
-
January 12th, 2009, 04:42 AM
#6
Re: exception in the destructor
I'd like to give this a try.
The "catch" in the question is how it's asking "can not", rather than "should not".
Local objects get destroyed during stack unwinding,
which means the memory used by it gets freed,
which in turn means, a desctructor is run to free the memory,
which also might contain a throw.
So, while you're correct,
the best answer would be D.
I'm not too sure tho.
-
January 12th, 2009, 04:51 AM
#7
Re: exception in the destructor
Yeah IC why U pick D!
I am with U
By the way the link U advertise is nice, I will ask my school again for contracts. we are lloking for c++ training orginatiation to help students laern
-
January 12th, 2009, 05:01 AM
#8
Re: exception in the destructor
Originally Posted by Belzeski
By the way the link U advertise is nice, I will ask my school again for contracts. we are lloking for c++ training orginatiation to help students laern
The questions are rather good, in terms of asking particular things one overlooks, doesn't quite know, or doesn't bother to learn. But I must say some of the wording is dumb/ambiguous - but its good to think about the answers to these type of questions anyway.
"What comes around, goes around"
-
January 12th, 2009, 05:07 AM
#9
Re: exception in the destructor
No exception in destructor.
Thanks for your help.
-
January 12th, 2009, 05:25 AM
#10
Re: exception in the destructor
C++ will allow you to throw exceptions and let them leave the destructor, but you shouldn't do it!
Lets say you have an object called Bill that throws an exception, causing the stack to immediately start to unwind. All the stack frames between Bill and the catch statement will be popped. That's fine, there is one exception and one catch statement, but what happens if one of the objects (Bob) in one of the stack frames being popped prior to the catch statement throws another exception which is allowed to leave its destructor? The catch statement can only catch one exception, but there are now two exceptions, so which one should catch catch? If it catches the one thrown by Bill, it will lose the one thrown by Bob, but who is to say that the one thrown by Bill is more important to the flow of the program? Conversely, it could lose Bills exception and catch Bob's, but who is to say that is the correct choice? Faced with this dilemma, the compilers only solution according to the C++ standard is to call terminate().
You should read this:
http://www.parashift.com/c++-faq-lit....html#faq-17.3
Last edited by PredicateNormative; January 12th, 2009 at 05:29 AM.
-
January 12th, 2009, 06:11 AM
#11
Re: exception in the destructor
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 12th, 2009, 03:25 PM
#12
Re: exception in the destructor
Originally Posted by PredicateNormative
C++ will allow you to throw exceptions and let them leave the destructor, but you shouldn't do it!
Right, so either the question is simply using improper english or the answer is none of the above. You certainly can throw an exception from a destructor or allow one to be rethrown, even though as you've indicated that is not a good idea. The use of the word cannot in that question is improper.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|