Google's Go & Exceptions

Nick Sabalausky a at a.a
Wed Jan 27 12:47:19 PST 2010


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.

>- Outside of constructor, there is no problem because the destructor
>is called.

1. Not always true in D (though that's a more general problem with D 
destructors).
2. There can be times when some cleanup would be needed that isn't in a 
destructor (especially in D since destructors can't be relied on actually 
being called.)





More information about the Digitalmars-d mailing list