Throwing Exception in CTOR and Smart Pointers

Posted by David Relihan on Stack Overflow See other posts from Stack Overflow or by David Relihan
Published on 2010-06-09T10:44:54Z Indexed on 2010/06/09 11:02 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

Is it OK to have the following code in my constructor to load an XML document into a member variable - throwing to caller if there are any problems:

   MSXML2::IXMLDOMDocumentPtr m_docPtr; //member


Configuration()
{
    try
    {                      
        HRESULT hr = m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument40));     

         if ( SUCCEEDED(hr))
         {
            m_docPtr->loadXML(CreateXML());
         }
         else
         {
            //throw exception to caller
          }
    }
    catch(...)
    {
         //throw exception to caller
    }

}

Based on Scott Myers RAII implementations in More Effective C++ I believe I am alright in just allowing exceptions to be thrown from CTOR as I am using a smart pointer(IXMLDOMDocumentPtr).

Let me know what you think....

© Stack Overflow or respective owner

Related posts about c++

Related posts about constructor