www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16388] New: Throwing constructors must destroy fully

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

          Issue ID: 16388
           Summary: Throwing constructors must destroy fully constructed
                    fields
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrei erdani.com

Per
http://www.digitalmars.com/d/archives/digitalmars/D/What_is_going_on_here_257862.html,
if the constructor of an object constructs a field and then throws an
exception, the field's destructor is not called.

import std.stdio;

struct A {
     int a = 3;

     this( int var ) {
         a += var;
     }

     ~this() {
         writeln("A down ", a);
     }
}

struct B {
     A a;

     this( int var ) {
         a = A(var+1);
         throw new Exception("An exception");
     }
}

void main()
{
     try {
         auto b = B(2);
     } catch( Exception ex ) {
     }
}

The code must print "A down".

--
Aug 14 2016