title: RAII approach to catching constructor exceptions
body: <p>I have a class that can throw an exception in its constructor. How can I declare an instance of that class in a try/catch block, while still making it available in the right scope?</p>
  
  <pre><code>try { MyClass lMyObject; }
  catch (const std::exception&amp; e) { /* Handle constructor exception */ }
  
  lMyObject.DoSomething(); // lMyObject not in scope!
  </code></pre>
  
  <p>Is there an alternative way to accomplish this, while respecting the <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> idiom?</p>
  
  <p>I'd prefer not to use an <code>init()</code> method for two-phased construction. The only other thing I could come up with was:</p>
  
  <pre><code>MyClass* lMyObject;
  
  try { lMyObject = new MyClass(); }
  catch (const std::exception&amp; e) { /* Handle constructor exception */ }
  
  std::shared_ptr&lt;MyClass&gt; lMyObjectPtr(lMyObject);
  lMyObjectPtr-&gt;DoSomething();
  </code></pre>
  
  <p>Works OK, but I'm not happy with the raw pointer in scope and pointer indirection. Is this just another C++ wart?</p>