www.digitalmars.com         C & C++   DMDScript  

D - auto/double destruction bug?

reply "Scott McCaskill" <scott mccaskill.org> writes:
While investigating how D behaves when an exception is thrown from a
destructor, I came across the following "interesting" behavior:

class MyClass
{
    ~this()
    {
        printf("~MyClass\n");
        throw new Exception("exception from dtor");
    }
}

int main (char[][] args)
{
    try
    {
        MyClass c = new MyClass;
    }
    catch (Exception e)
    {
        printf("caught exception: %.*s\n", e.toString());
    }

    return 0;
}

output:
~MyClass
caught exception: exception from dtor
~MyClass
<crash>

If I remvoe the "auto", I get this:
~MyClass
<crash>

I looked through the documentation, but couldn't find enough information to
allow me to deduce what the above program ought to do.  More info on the
behavior of exceptions in D would really be helpful, especially for C++
programmers trying to figure out how to write exception-safe code in D :)

Also, has any consideration been given to adding something akin to throw()
as in C++?  Preferably compiler-enforced, unlike C++.  In C++ (and, I
suspect, in D) it's not possible to write exception-safe code unless you can
be sure that certain operations cannot throw.  It seems like a
compiler-enforced nothrow specification could be really useful for that.

-- 
Scott McCaskill
Aug 30 2003
parent "Scott McCaskill" <scott mccaskill.org> writes:
 int main (char[][] args)
 {
     try
     {
         MyClass c = new MyClass;
Doh. Of course, the above should be "auto MyClass c = new MyClass;".
     }
     catch (Exception e)
     {
         printf("caught exception: %.*s\n", e.toString());
     }

     return 0;
 }
Aug 30 2003