CPB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exceptions in constructors



I will second what Steve said with one "exception" :-).  If you look at
Stroustrup's gray book:  "The C++ Programming Language" chapter 9 you'll
see that you can safely throw exceptions within a constructor if you adopt
a "resource acquisition is initialization" approach (or pattern if you
prefer the term).  The problem is that you don't have any control over how
this was done in the IDE code where the problem (probably a partially
constructed object) is occurring.

Frank

At 06:22 PM 8/27/97 -0400, Steve Crocker wrote:
>> 
>> Has anyone else tried to add a throw statement to a component constructor?
>> 
>
>I personally haven't tried that in Builder. However, throwing an
>exception from a constructor is basically a bad thing, because
>destructors only get called for objects that are full created, eg, their
>constructors finish.
>
>Even though components have to be created on the heap and therefore
>would not benefit from destructor cleanup in exception handling, it
>probably isn't a good idea to get into the habit of throwing exceptions
>from constructors.
>
>As far as why it blows up, I do know that in BC++ v5.02 I had all kinds
>of problems with exceptions when 'Enable destructor cleanup' was on.
>Disabling that seemed to fix the problem, although kind of ruins one of
>the reasons for using exceptions. 
>
>I honestly don't remember if you can turn that option on/off from the
>BCB IDE, but you might try checking for it...
>
>Steve
>
>

************************************
           Frank Solomon
       University of Kentucky
PGP Public key available via finger
   http://www.uky.edu/~sysfrank
------------------------------------
        Life begins at 180
************************************



W Komornicki's Home Page | Main Index | Thread Index