19.3.4 Memory Leak in a Constructor (cont.)

Here's one solution:

DataArray::DataArray(int s)
{
   data = new int[s];
   try
   {
      init(s);
   }
   catch (...) // Catch any exception init throws
   {
      delete[] data;
      data = NULL;
      throw; // Rethrow exception
   }
}

prev |top |next