Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!decwrl!ie0013.dec.com!vilot From: vilot@ie0013.dec.com (Michael J. Vilot) Newsgroups: comp.lang.c++ Subject: Re: Errors in Constructors Message-ID: <8902011503.AA10122@decwrl.dec.com> Date: 1 Feb 89 18:05:00 GMT Organization: Digital Equipment Corporation Lines: 34 > how to handle an error situation in the constructor for the class. > ... I would like to provide some way for the program > creating the object to detect this and handle it appropriately. How > can I go about doing this? > mutchler@sun.UUCP > sun!zule!mutchler I use the approach of storing status information in the object (originally outlined by Jon Shopiro in note <7188@alice.UUCP> 17 Aug 87). If there's a possibility that a constructor can fail, then the notion of a "null" object of that class -- that is, a well-formed object that simply has no interesting data values -- is an essential part of the abstraction provided by the class. This gives clients of the class an abstract way to test if the constructor succeeded (similar to testing for a NULL pointer after calling malloc() in old C). Providing member functions for querying the status rounds out the idea -- and delegates the details of how to react to the situation to the client. An alternate approach is to invoke an error reporting function, although I tend to avoid this in constructors. See pp. 207-209, and the discussion of the _new_handler on pp. 92/93 in "The C++ Programming Language" for an outline of this alternative. Hope this helps, Mike Vilot Self-employed. Consulting at: e-mail: vilot%csdpie.dec@decwrl.dec.com when that fails: (508)467-3631 [for now]