From 7551762965205472218
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,9dc775cb38879a6d
X-Google-Attributes: gid109fba,public
X-Google-Thread: 12808a,9dc775cb38879a6d
X-Google-Attributes: gid12808a,public
X-Google-Thread: ffc26,9dc775cb38879a6d
X-Google-Attributes: gidffc26,public
X-Google-Thread: f78e5,9dc775cb38879a6d
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1994-05-17 20:37:30 PST
Xref: gmd.de comp.sys.amiga.programmer:38190 comp.lang.c++:55631 comp.std.c++:6893
Path: gmd.de!nntp.gmd.de!urmel.informatik.rwth-aachen.de!newsserver.rrzn.uni-hannover.de!ina.zfn.uni-bremen.de!marvin.pc-labor.uni-bremen.de!news.uni-stuttgart.de!news.belwue.de!zib-berlin.de!zrz.TU-Berlin.DE!netmbx.de!Germany.EU.net!EU.net!howland.reston.ans.net!wupost!gumby!newsxfer.itd.umich.edu!zip.eecs.umich.edu!umn.edu!dawn.mmm.com!hopps
From: hopps@mmm.com (Kevin J. Hopps)
Newsgroups: comp.sys.amiga.programmer,comp.lang.c++,comp.std.c++,sunyab.cs.c++
Subject: Re: error handling in constructors and overloaded operators
Followup-To: comp.sys.amiga.programmer,comp.lang.c++,comp.std.c++,sunyab.cs.c++
Date: 17 May 1994 14:30:27 GMT
Organization: 3M - St. Paul, MN  55144-1000 US
Lines: 66
Message-ID: <2rake3$egd@dawn.mmm.com>
References: <Cpv1w6.33o@acsu.buffalo.edu>
Reply-To: kjhopps@mmm.com
NNTP-Posting-Host: 130.99.69.63
X-Newsreader: TIN [version 1.2 PL2]

JOSEPH F. HART (vishart@ubvms.cc.buffalo.edu) wrote:
> Greetings...

>      I am relatively new to C++, having learned it on my own after
> programming in C for many years.  I am converting some of my source
> files to C++ from C, and I have a few questions.  These are as
> follows:

>      Consider the following short program, where the member "el2"
> points to a dynamically allocated buffer :

> class cls1 {
>    int el1;
>    char *el2;
> };

> main()
> {
>    cls1 obj1cls1, obj2cls1;
>    ...
>    obj2cls1=obj1cls1;
>    ...
> }

>    Suppose the constructor should fail for some reason, such as a low
> memory condition during the "el2" buffer allocation.   What is the
> best way of indicating constructor failure so that action may be taken
> by the main program ?

IMHO, the best way is to throw an exception.

If your compiler does not yet support exceptions, I've seen various
ways of indicating an error during construction.  One such way is to
write a member function ok() which returns true if the object is ok
to use.

>    Suppose the overloaded assignment should fail for some reason, such
> as a low memory condition when allocating a new buffer for
> "obj2cls1.el2".  What is the best way of indicating assignment failure
> so that action may be taken by the main program ?

Same answer as above.

> Several solutions seem to suggest themselves to me, but I have
> concerns about them.  Some of these solutions are :
> Global error flag :
>      I prefer to avoid the use of global variables whenever possible.

This is wise, globals create reentrancy problems.

> Argument to the constructor or assignment :
>      This one is easy to implement for constructors, but could be
> difficult to implement for overloaded operators like the assignment.
> There seems to be no place to put an argument to the assignment.

You've discovered a very good reason for using exceptions -- when
there is no other [convenient] way to report the error.

> Error flag internal to the object :
>      If the object itself is to be dynamically allocated, and the
> allocation fails, there is no object to hold the flag to be tested.

If dynamic allocation (operator new) fails, it will throw an exception
if the compiler supports exceptions.  Otherwise it will return zero.
Either way, you should know about the failure.  But object assignment
and dynamic allocation of objects are separate operations.


