From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6b1a1ed8b075945 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!out01a.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!in03.usenetserver.com!news.usenetserver.com!pc03.usenetserver.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Allocators and exceptions References: <1189323618.588340.87180@o80g2000hse.googlegroups.com> <1189369871.672082.162750@50g2000hsm.googlegroups.com> <1189460936.295604.143720@r29g2000hsg.googlegroups.com> <1189502377.626510.172690@22g2000hsm.googlegroups.com> <1189601170.835400.72630@w3g2000hsg.googlegroups.com> From: Stephen Leake Date: Thu, 13 Sep 2007 07:51:05 -0400 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/22.1 (windows-nt) Cancel-Lock: sha1:NXgWMQJmPVhn2zX4RaqPAg6St/Y= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 8c11046e9242ce05e48ed19966 Xref: g2news2.google.com comp.lang.ada:1941 Date: 2007-09-13T07:51:05-04:00 List-Id: Maciej Sobczak writes: > On 12 Wrz, 14:29, Stephen Leake > wrote: > >> The rule should be: >> >> Constructors should not propagate exceptions up; they must handle >> all exceptions internally. > > No. > What should the constructor do *after* handling the exception? > Leave the object half-baked? > Do you want to introduce additional states to the object design just > to handle the "oops I'm not initialized" case? > > The only reasonable solution is probably to use custom factories > instead of built-in allocators. > > But... can we prohibit the user from using the allocator for the given > type? Hmm. You seem to be equating "allocator" with "constructor"; I was not. I thought you meant "custom factory" when you said "constructor". Hmm. Maybe you mean only Finalization.Initialize by "constructor"; that is called by "new". In any case, I'll modify my statement; Constructors (ie Initialize or user-built factory functions) must handle all exceptions internally. If in some situation the object cannot be properly constructed, any memory allocated should be deallocated, all other appropriate cleanups done, and then a final "Failed" exception should be raised to indicate the failure to the caller. The point is that the caller of the constructor either gets a properly initialized object, or a single well-defined exception indicating that the object could not be created. In the latter case, all proper cleanup is already done by the time the final exception is raised. I think that is consistent with what you want to have happen. If the constructor is designed to return an access value, an alternative to raising the final exception is to return null, but an exception is probably better. -- -- Stephe