Is it supposed to be safe to throw inside struct destructors?

Marco Leise Marco.Leise at gmx.de
Mon Apr 7 01:33:28 PDT 2014


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.

If we CAN throw in ~this(), the exception should (based on
the fact that the program is now in an invalid state, like
after a failed assertion or out-of-memory) be wrapped in
something like a FinalizeError to tear down the whole program.

If not, then error reporting needs to be done in a "nothrow"
context. This has far reaching consequences since typically
error logging means doing I/O and I/O can fail and throw
Exceptions. So either the logging calls in destructors
would be wrapped in try-catch or there needs to be some
printf() calls scattered in ~this().


References:
http://wiki.dlang.org/DIP44
http://forum.dlang.org/thread/op.vvek7te7tuzx1w@cybershadow.mshome.net?page=3#post-iqjhlc:241rde:241:40digitalmars.com
http://d.puremagic.com/issues/show_bug.cgi?id=4621

-- 
Marco



More information about the Digitalmars-d mailing list