goto skips declaration of variable

nrgyzer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 21 11:44:29 PDT 2014


On Thursday, 21 August 2014 at 17:39:16 UTC, Ali Çehreli wrote:
> On 08/21/2014 04:12 AM, nrgyzer wrote:
>
> > I'm using the goto-command to exit my function
> > if an error (not necessarily an exception) occured.
>
> Sorry to repeat myself but if an exception occurs in code 
> before the goto, the exit code will not be executed. Of course, 
> it may be that the function is defined 'nothrow' so my concern 
> does not apply.
>
> > Sure, I can
> > simply do a return, but I personally prefer a fixed endpoint
> of
> > my functions.
>
> Again, that is not possible in a language that has exceptions.
>
> Ali

Sure, but what about the following pretty simple source:

bool testa(int a)
{
     bool fIsValid = (a > 0);

     if (a > 0)
        goto Exit;

     throw new Exception("int <= 0");
Exit:
     return fIsValid;
}

int main(string[] args)
{
     testa(10); // Use goto
     testa(-1); // Throws an exception
}

I'm absolutely aware that gotos are useless if an exception
occured before the goto appears. But in some cases they are very
useful, especially when I want prevent 10 ident-steps or
something else...

Okay, my question was answered -> compiler bug. Thanks in advance!


More information about the Digitalmars-d-learn mailing list