how to handle exception in constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    how to handle exception in constructor

    hi.. if a class A contains an array of objects of another class B.. And in the constructor of A, while constructing objects of B, any one object throws an exception, then how can we guarantee to release all the memory acquired.. so that there is no memory leak..

    It was asked in an interview recently.. I was not very sure how to answer this.. can any one help please...

    Code:
    class B{};
    class A{
    public:
      B Array[100];
      ...
    };
    In the above code, if in constructor of A, suppose 99 objects of B are constructed successfully, but 100th object throws exception, then how can we guarantee to release all the memory acquired by the other 99 objects?
    Last edited by bajajv; Apr 14 '12, 06:48 PM. Reason: adding more details
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    If you have an exception inside any function, then that function terminates. That means any memory you allocated in that function will not get released.

    So what you do is catch any exceptions while still inside the function and then release any memory in the catch block. Thereafter, you re-throw the exception:

    Code:
    void MyFunction(A*& ptr)
    {
         ptr = 0;  //the pointer to the allocated memory
        try
        {
           ptr = CreateArray();    //maybe a throw here
        }
        catch (...)
        {
           delete [] ptr;  //release allocated memory
           ptr =0;
           throw;          //re-throw original eception
        }
    
    }
    However your example:
    Code:
    class A{
     public:
       B Array[100];
       ...
     };
    does not apply. It would be the responsibility of B's ctor to do any exception handling. Class A has no responsibility for B. Class A has a stack array of B so it is the compiler who will manage the memory for that array.

    Remember in your example: 1) complier allocates a class A object and that includes the 100 element array of B, 2) the compiler calls the default ctor of B for each array element (this is where the exception might occur, and then 3) compiler calls ctor for class A. Nowhere in here do you have any responsibility for memory management.

    But let's assume you know the coders for B are a shoddy bunch and you need to protect yourself. In this case you create your A object on the heap and if it doesn't create properly, you delete it:

    Code:
    int main()
    {
       A* ptr = 0;   //the address of our A object
       try
       {
          ptr = new A;
       }
       catch (...)
       {
          delete A;    //creation failed
          ptr = 0;     //set our pointer to A to zero
       }
    etc...
    
    }
    At the etc... you are still running. If ptr is zero, then you have trouble but you can re-try to create the A object or advise your user that you have to terminate or perhaps advise that the portions of the program requiring ptr have been rendered inactive.

    Comment

    Working...