At 04:46 PM 5/5/99 -0500, Alan wrote:

>I've got a constructor that does some memory allocation and creates a
>record for the app, etc. I am now trying to add nice error handling if
>the alloc's fail in the constructor, and I am at a loss for how to do
>this. Is this where you use exceptions in C++?

Alan,

The short answer is yes. However, in some cases, you have to very careful
on how you use exception-handling in the constructor, as you can easily
create resource leaks. I don't want to confuse you with the details since
you're just starting. =-) The problem is what to do when the new object
is only "partially" constructed.

One alternative is have the constructor do only things which you know 
cannot fail, and move out everything else to an initialize function,
returns a success/error code. E.g., so you can do:

   CMyForm formX;  // No worries here, construction always works.

   int error = formX.Initialize(); // Might fail, but we can check.
   if (error)
   {
      handle_error();
   }

Regards,

-Ade

Reply via email to