Nothrow functions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Oct 2 05:21:38 PDT 2008


Janderson wrote:
> Andrei Alexandrescu wrote:
>> dsimcha wrote:
>>> == Quote from Janderson (ask at me.com)'s article
>>>> Walter Bright wrote:
>>> http://www.reddit.com/r/programming/comments/74fx4/nothrow_functions_in_the_d_programming_language/ 
>>>
>>>> Perhaps now constructors can enforce no-throw.  Functions that have
>>>> throw would have to be handled in that constructor.  Of course we could
>>>> always do this manually, but it might be worth considering making on by
>>>> default for constructors.
>>>> -Joel
>>>
>>> Please, please, please, please, *please* no!!!  Anything that is in 
>>> any way
>>> similar to checked exception Hell in Java does not belong in D.  
>>> Nothrow is a
>>> great feature precisely because, by being a contract that is only 
>>> enforced when
>>> the programmer explicitly asks for it to be, it can be simply ignored 
>>> in places
>>> where one doesn't want to use it.  Making nothrow the default in 
>>> constructors
>>> really smacks of Java-style bondage and discipline, and a major 
>>> reason why I use D
>>> is to avoid such things.  If nothrow is the default *anywhere*, it 
>>> will lead to
>>> aggravation and error swallowing similar to Java's checked exceptions.
>>
>> I agree. Bondage and discipline, heh :o). I'm actually surprised at 
>> the desire of making most constructors nothrow. Why?
>>
>> Andrei
> 
> If an exception fires during the construction of an object and you don't 
> handle it your left with a partially formed object.  It becomes 
> difficult to then make that object an invariant.  This is particularly 
> bad when it occurs in the base classes constructor.

But C++ solved this by making it impossible to obtain a 
partially-constructed object. If the constructor throws, there's never 
an object to talk about.

Andrei



More information about the Digitalmars-d mailing list