Re: exiting a constructor early
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 03/12/04
- Next message: Francis Glassborow: "Re: breaking a while loop?"
- Previous message: Hendrik Schober: "Re: Passing Pointers"
- In reply to: Dan Moos: "exiting a constructor early"
- Next in thread: Francis Glassborow: "Re: exiting a constructor early"
- Reply: Francis Glassborow: "Re: exiting a constructor early"
- Reply: Dan Moos: "Re: exiting a constructor early"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Francis Glassborow: "Re: breaking a while loop?"
- Previous message: Hendrik Schober: "Re: Passing Pointers"
- In reply to: Dan Moos: "exiting a constructor early"
- Next in thread: Francis Glassborow: "Re: exiting a constructor early"
- Reply: Francis Glassborow: "Re: exiting a constructor early"
- Reply: Dan Moos: "Re: exiting a constructor early"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|