The scope of scope(exit)

Walter Bright newshound at digitalmars.com
Thu Mar 9 21:15:39 PST 2006


"Derek Parnell" <derek at psych.ward> wrote in message 
news:1amv63k5pimu6.q5fyzezd5ew5.dlg at 40tude.net...
> The following code did not work as I expected it to.

Think of it like:

int Foo(int x)
{
   int y;
   y = x;
   try
   {
        return y;
    }
    finally
    {
          if (x < 5) y += 9;
     }
}

which should make it clear. The reason it works this way is because the 
return expression can be arbitrarilly complicated, and can throw exceptions. 
So the exit code must be executed *after* the return value is computes. 





More information about the Digitalmars-d-learn mailing list