Is it supposed to be safe to throw inside struct destructors?
monarch_dodra
monarchdodra at gmail.com
Mon Apr 7 14:36:34 PDT 2014
On Monday, 7 April 2014 at 19:19:18 UTC, Dmitry Olshansky wrote:
> 07-Apr-2014 12:33, Marco Leise пишет:
>> I would like to hear a definite answer on this. A failure in
>> ~this() leaves the program in an undefined state. You cannot
>> back out of returning from a function that needs to perform
>> cleanup on stack structs. Constructors do not have this
>> problem. With scope(failure) you can guard all resources and
>> guarantee cleanup after an exception is thrown.
>
> There is exception chaining for that. All collateral exceptions
> get appended to a list. IIRC throw in a destructor while
> exception is in flight breaks out of this particular
> destructor, other if any are then executed in turn.
That's the theory, but in practice, throwing an exception
allocates, and you can't allocate if you are in a GC collect
cycle.
So it's kind of back to square 1 in terms of throwing in
destructors: Don't do it :/
More information about the Digitalmars-d
mailing list