0

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 MyException("Could not create instance of Dom");
          }
    }
    catch(...)
    {
         LogError("Exception when loading xml");
         throw;
    }

}

Based on Scott Myers RAII implementations in More Effective C++ he cleanups if he alocates any resources i.e. pointers:

BookEntry::BookEntry(const string& name,
                 const string& address,
                 const string& imageFileName,
                 const string& audioClipFileName)
: theName(name), theAddress(address),
  theImage(0), theAudioClip(0)
{
  try {                            // this try block is new
    if (imageFileName != "") {
      theImage = new Image(imageFileName);
    }

  if (audioClipFileName != "") {
      theAudioClip = new AudioClip(audioClipFileName);
    }
  }
  catch (...) {                      // catch any exception


    delete theImage;                 // perform necessary
    delete theAudioClip;             // cleanup actions


    throw;                           // propagate the exception
  }
}

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....

Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85
  • 4
    Why the `try { ... } ... catch(...) {}` ? Can fix the code so that it actually looks like a constructor definition, otherwise it's a bit difficult to give constructive criticism. – CB Bailey Jun 09 '10 at 10:46
  • I do not understand you problem. It is generally OK to throw exceptions from constructors. What is your concern? What alternatives do you consider? – Björn Pollex Jun 09 '10 at 10:56
  • You are worried about whether the `m_docPtr` variable gets destructed or not when you throw exception? – Naveen Jun 09 '10 at 11:01
  • What are your concerns? What negative consequences do you suspect? – sharptooth Jun 09 '10 at 11:05
  • The code that you've attributed to Scott Meyers is an illustrative example from half way through Item 10 of MEC++. You should carry on reading to the end of Item 10 where he presents a much simpler and more robust solution using `const auto_ptr` . – CB Bailey Jun 09 '10 at 11:44
  • @Charles Yep - I have. I'm assuming that the fact that IXMLDOMDocumentPtr is a smart pointer means I am in the same situation. COM == Doubt! – Robben_Ford_Fan_boy Jun 09 '10 at 12:22
  • 1
    @David Relihan: COM has nothing to add here. A smart pointer is yet another class and is treated like yet another class. – sharptooth Jun 09 '10 at 12:35

2 Answers2

1

C++ guarantees that in case of an exception all fully constructed objects will be destroyed.

Since m_docPtr is a member of class Configuration it will have been fully constructed before the class Configuration constructor body begins, so if you throw an exception from class Configuration body as you intended in your first snippet m_docPtr will be destroyed.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

Do you plan to do anything in the catch block? If nothing, you probably do not need the try catch. On windows, I believe that catch(...) catches hardware interrupts (experts please correct), something to keep in mind.

Raam
  • 10,296
  • 3
  • 26
  • 27