August 30, 2003 auto/double destruction bug? | ||||
---|---|---|---|---|
| ||||
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
|
August 30, 2003 Re: auto/double destruction bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Scott McCaskill | > 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; > } |
Copyright © 1999-2021 by the D Language Foundation