title: How to delete object if constructor throws an exception?
body: <p>So we have a constructor that can throw an exception depending on the arguments passed to it, but we do not know how to delete the object if this occurs. Important part of the code:</p>
  
  <pre><code>try
  {
      GameBase *gameptr = GameBase::getGame(argc, argv);
      if (gameptr == 0)
      {
          std::cout &lt;&lt; "Correct usage: " &lt;&lt; argv[PROGRAM_NAME] &lt;&lt; " " &lt;&lt; "TicTacToe" &lt;&lt; std::endl;
          return NO_GAME;
      }
      else
      {
          gameptr-&gt;play();
      }
      delete gameptr;
  }
  catch (error e)
  {
      if (e == INVALID_DIMENSION)
      {
          std::cout &lt;&lt; "Win condition is larger than the length of the board." &lt;&lt; std::endl;
          return e;
      }
  }
  catch (...)
  {
      std::cout &lt;&lt; "An exception was caught (probably bad_alloc from new operator)" &lt;&lt; std::endl;
      return GENERIC_ERROR;
  }
  </code></pre>
  
  <p>In the third line, <code>GameBase::getGame()</code> calls the constructor for one of the games derived from <code>GameBase</code> and returns a pointer to that game, and these constructors can throw exceptions. The question is, how can we then delete the (partial?) object pointed to by <code>gameptr</code> if this occurs? If an exception is thrown, we will exit the scope of <code>gameptr</code> because we leave the try block and cannot call <code>delete gameptr</code>.</p>