Google's Go & Exceptions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 27 13:00:13 PST 2010


Nick Sabalausky wrote:
> Nick Sabalausky wrote:
>>> ""J?r?me M. Berger"" <jeberger at free.fr> wrote in message
>>> news:hjnrhv$1l0k$1 at digitalmars.com...
>>>> Throwing exception inside constructors should be avoided because
>>>> then the destructor is never called and you risk leaking like crazy.
>>> That's a risk outside of constructors too. Hence:
>>>
>>> scope(failure) {}
>>>
>> - This doesn't exist in C++;
> 
> Well, I was really only talking about D. I guess the answer to "Should 
> throwing exceptions in a constructor be avoided?" is "Depends on the 
> language." For C++, constructors have historically had a number of tricky 
> edge cases (though I couldn't really remember what), so I'm not surprised 
> that throwing exceptions is one of them. For D though, I'm not sure I see 
> that there's a problem.

The edge cases involve member initialization that throws. The root of 
the matter is that in C++ you can't have a type with a guaranteed 
nothrow constructor. That makes a lot of things very difficult when it 
comes to object composition.

I've aired a number of times the idea that the parameterless constructor 
should not throw. That would simplify many issues, but there are still 
cases when that doesn't work (nonull!!!)


Andrei



More information about the Digitalmars-d mailing list