scope attribute and catching exception

Paolo Invernizzi arathorn at fastwebnet.it
Mon Oct 13 02:50:46 PDT 2008


Hi all,
Someone can explain me why the scope attribute of r2 is not satisfied? It's a bug or an expected behaviour?

Paolo

module tests.d.scopes.t02.test;

class Resource {
    static int allocated = 0;
    this( bool mustThrow = false ){
        allocated ++;
        if( mustThrow ) throw new Exception("bang");
    }
    ~this(){
        allocated --;
    }
}

void main(){
    
    
    // Why the destructor of r2 is not called when exiting the scope?
    {
        try {
            scope r1 = new Resource();
            scope r2 = new Resource(true);
        }
        catch(Exception e){}
    }
    //assert( Resource.allocated == 0); // Expected
    assert( Resource.allocated == 1 ); // What's happening
    
}


More information about the Digitalmars-d-learn mailing list