Re: exiting a constructor early

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 03/12/04


Date: Fri, 12 Mar 2004 22:09:39 +1100


"Dan Moos" <dan.moos@verizon.net> wrote in message
news:6X34c.103514$C65.63496@nwrddc01.gnilink.net...
| What it is the common way to exit a constructor early, due to an error?
|
| Does one throw an exception? I mean I know that would work, I'm just curious
| as to what the various methods are.
|
| Also, what exactly happens to any dynamically allocated memory that was
| obtained before the exception was thrown?

The problem with this scenario, is that if an exception occurs
(or is deliberately thrown during construction), the destructor
will not be invoked, so you would have to find a design that
can combat this behaviour - smart pointers are a good start :-).

| I've heard that either an object is fully constructed, or not
| at all,

No, that's not quite accurate.
An object can indeed be 'partially' constructed too.

| so does this mean that the memory gets returned, or do
| I have to handle that when I catch the exception.

It means that 'automatic' sub objects of an partially constructed
object, that were constructed, will have their destructors invoked,
but for memory that your object attempts to allocate in the constructor,
you would have to somehow handle that memory in an appropriate handler,
if available - meaning, free it up yourself, or use smart pointers as
mentioned earlier.

There is more to it that that of course, but that's the basic picture.
You can download some good e-books - if you don't already have them, or
even check the Marshal Cline C++ FAQs.

| I'm new to using exception handleing (throw and catch and what not), so I
| also wonder, what are some basic criteria for determining where the
| exception is "caught"

If you intend on allocating memory in the constructor initialiser
list, then (and if your compiler supports it), you should use what's
known as a function try block. This is preferable over a normal try
catch arrangement, because the exception can be handled before the
constructor body has been entered.

Exceptions are a fairly large topic (though not all that complicated),
and they're not something that can be fully covered in an post such as
this :-).

Do you have any books to refer to ?

Cheers.
Chris Val



Relevant Pages

  • Re: Using form controls from other classes.
    ... Form's constructor creates A, whose constructor in turn creates C. ... button on Form when C catches the exception. ... handler because I add the handler after creating the objects. ... C is created from A when the Form first loads. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: new keyword
    ... Call constructor for object placed into allocated memory ... If an exception is thrown from the constructor, handle it, cleanup ... re-throw the exception up the stack. ... instantiate one in an available subblock. ...
    (microsoft.public.vc.mfc)
  • Re: Allocators and exceptions
    ... of an object never propagates an exception, ... I guess I had misunderstood what's meant by 'constructor', ... the leak in this case doesn't bother me too much, ... very long-running applications are likely to have problems with memory ...
    (comp.lang.ada)
  • Re: is such exception handling approach good?
    ... here if you get a memory exception in m_pConn you get a memory leak in ... m_pBuff since the dtor will not be called. ... has been created you can't clean it up in your exception handler. ... of them in constructor; sometimes all of them. ...
    (microsoft.public.vc.language)
  • Re: new keyword
    ... all required memory (what happens or which exception is thrown depends ... configured to have "new" return NULL if it fails to allocate. ... by the underlying object's constructor will be destructed, ... memory allocated by "new" will be freed. ...
    (microsoft.public.vc.mfc)