CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    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"

  2. #2
    Join Date
    Nov 2007
    Posts
    74

    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.

  3. #3
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    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"

  4. #4
    Join Date
    Nov 2007
    Posts
    74

    Re: exception in the destructor

    Where is the url ? I 'd like to learn too

  5. #5
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    Re: exception in the destructor

    "What comes around, goes around"

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    37

    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

  8. #8
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    30

    Re: exception in the destructor

    Quote 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"

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: exception in the destructor

    No exception in destructor.
    Thanks for your help.

  10. #10
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    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.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  12. #12
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: exception in the destructor

    Quote Originally Posted by PredicateNormative View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured