Thread: Excaption in Derived Constructors?

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Question Excaption in Derived Constructors?

    I have a question regarding exceptions in contructors.

    Picture that I have the following class:

    Code:
    class BaseObject
    {
    public:
      BaseObject();
      virtual ~BaseObject();
    };
    Now I have a simple derived class as follows:

    Code:
    class DerivedObject : public BaseObject
    {
      DerivedObject();
      virtual ~DerivedObject();
    };
    What happens if an exception is thrown within the constructor of the DerivedObject, like so:

    Code:
    DerivedObject::DerivedObject()
      : BaseObject()
    {
      // Contructor - do something
      try
      {
        ...
        ...
      
        // EXCEPTION THROWN HERE
        ...
      }
      catch(...)
      {
        // Free newly created heap objects
      }
    }
    I am familiar with catching exceptions within constructors to prevent memory loss. However, in this specific case, will the destructor of the BaseObject be called automatically after the exception is thrown?

    My guess is that it won't, but I'm not sure. Should I ensure that I free any memory held by BaseObject in the catch block of DerivedObject constructor?

    Edit : Oops there was an exception in the title of this thread.
    Last edited by Davros; 10-30-2002 at 08:01 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The base object will have its constructor called and will be totally constructed before the derived constructor is called. As the base object is contructed, it's destructor is called if you catch the exception outside of the class...so yes, base will be freed...

    Unfortunately, a destuctor will only be called on a fully constructed object, so you may find your problems will be with derived if you have allocated resources in the construtor before throwing the exception (assuming you cant rescue the situation by catching the exception in the constructor and allowing the constructor to return)...

    Scott Meyers, discusses this situation in More Effective C++ along with numerous workarounds and tips to avoid leaks from throwing exceptions in constructors.......

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks Fordy. So if I have the following code:

    Code:
    DerivedObject* dobj = new DerivedObject();
    and an exception is thrown in the DerivedObject constructor*, but after the BaseObject constructor has been completed (as above), then the BaseObject destructor will be called. Is this correct?

    *And the DerivedObject constructor re-throws the exception in the catch block, after it is cleared up any memory allocated.

    Do you follow that?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks to you both. I also tested it with some dummy code.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Excaption in Derived Constructors?

    Originally posted by Davros

    I am familiar with catching exceptions within constructors to prevent memory loss. However, in this specific case, will the destructor of the BaseObject be called automatically after the exception is thrown?
    Are you rethrowing the exception in the catch(...) handler?
    If not, then the constructor will return successfully and the object will be constructed.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Yes, but forgot to include it in my previous code samples.

    Thanks for the thought.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  3. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM
  4. Replies: 2
    Last Post: 01-15-2002, 06:00 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM