exception in the destructor

This is a discussion on exception in the destructor within the C++ Programming forums, part of the General Programming Boards category; Which of the following reasons describe why a destructor cannot throw an exception in C++? A. The C++ language does ...

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    B. If you consider that a bad thing Well, it is...
    C. That's probably somewhat subjective. It is illogical since the destruction must be completed (instance is considered dead for the rest of the program as soon as destructor is called, not when the destructor call finishes). On the other hand, why would it be illogical to want to find out if the destruction completed normally/successfully?
    D. Probably most likely reason.

    Also, the question should probably say shouldn't. You can throw from destructor, it is just a very bad thing to do.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It also misses some important points, e.g. you're not allowed (no diagnostic required) to put objects with throwing destructors into standard containers - or have them interact with any element of the standard library, pretty much.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by CornedBee View Post
    It also misses some important points, e.g. you're not allowed (no diagnostic required) to put objects with throwing destructors into standard containers - or have them interact with any element of the standard library, pretty much.
    If you absolutely have to have some kind of object which could fail to be destructed, you have to bite the bullet and use two-phase cleanup and specify that the destructor cannot be called until the object has been cleaned.

    You're stuck relying on programmer carefulness but I don't know a better way around the limitations.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  3. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 07:54 AM
  4. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 12:12 PM
  5. Destructor called after exception?
    By Carlos in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2003, 01:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21