[Issue 704] `class` destructor is called even if constructor throws

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 17 21:06:57 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=704

hsteoh at quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh at quickfur.ath.cx

--- Comment #11 from hsteoh at quickfur.ath.cx ---
Does this bug only apply for scope classes? I tested the following code on dmd
git master:

------
class MyClass {
        this() {
                writeln("ctor");
                throw new Exception("");
        }
        ~this() {
                writeln("dtor");
        }
}
void main() {
        try {
                scope c = new MyClass;
        } catch(Exception e) {
                // ignore
        }
}
------

Only the ctor is called, not the dtor, because the ctor throws and does not
properly construct the object. This is correct behaviour.

However, changing `scope` to `auto` will cause the dtor to still be called, in
spite of the ctor throwing an exception.  According to Andrei's comment, this
is wrong, since the object was not fully initialized, and the exception from
the ctor should be taken to mean that construction of the object has been
abandoned. Not sure if this belongs to this bug, though, or to a different bug,
since the original complaint involved "auto" classes, which I'm assuming was
the original name of scope classes.

--


More information about the Digitalmars-d-bugs mailing list