Behaviour of goto into catch blocks.

Don nospam at nospam.com
Sun Sep 5 00:33:22 PDT 2010


Iain Buclaw wrote:
> When it comes to using goto in D, the behaviour seems to be that you cannot enter a try block, and neither can you enter or exit from a
> finally block.
> 
> What about catch blocks? It seems that there is no restrictions imposed on them, meaning that the following is legal.
> 
> 
> void main()
> {
>     goto in_catch;
>     try
>     {
>         throw new Exception("msg");
>     }
>     catch (Exception e)
>     {
>   in_catch:
>          throw e;
>     }
> }
> 
> 
> As strongly as I feel that goto into catch blocks shouldn't be allowed, is this the intended behaviour of the language? If so, why?

Obviously that code should be rejected -- what would be thrown?
I think there's no intrinisic problem with a goto into a catch block, 
but in practice, it might as well be rejected, because it almost always 
involves bypassing a variable declaration. The spec says
"It is illegal for a GotoStatement to be used to skip initializations. "
though at present the compiler doesn't enforce this.

I would have thought that a finally block would be OK, but the spec says:
"A FinallyStatement may not exit with a goto, break, continue, or 
return; nor may it be entered with a goto."
So presumably going into a catch block is also supposed to be illegal.


More information about the Digitalmars-d mailing list