-5

I am particularly aware of C# Reference and have analysed why can't i use goto statement in finally blocks but i wonder when i am trying the same thing with using methods the i am finally able to leave the finally block but according to specification i must not. Isn't the goto moo and static void moo() doing the same act i.e taking me out of finally block?? So why in first case it is not working but working smooth in second case?

It is a compile-time error for a break, continue, or goto statement to transfer control out of a finally block. When a break, continue, or goto statement occurs in a finally block, the target of the statement must be within the same finally block, or otherwise a compile-time error occurs.

It is a compile-time error for a return statement to occur in a finally block.

First Case with Goto statement.

static void Main(string[] args)
            {
                try{}
                catch (Exception ex){}
                finally
                {
                    goto moo;
                }
            moo:
                Console.Write("Hello");
            }

Second case with Methods : it worked!

 static void Main(string[] args)
        {

            try{}
            catch{}
            finally
            {
                moo();
            }
        }
        static void moo()
        {
            int x=2, y=3, a=4, b=7;
            if (x == y)
                return;
            else
                a = b;
        }

The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
  • 6
    Is there *any* good reason that you want to use `goto` ? The two snippets have nothing in common. The second calls a method. The first tries to break out of the exception finalizer in an uncontrolled manner – Panagiotis Kanavos Oct 31 '16 at 16:26
  • Exactly what are you trying to achieve? You will get to the Console.Write without the goto. – PaulF Oct 31 '16 at 16:28
  • See the [accepted answer here](http://stackoverflow.com/questions/1952306/goto-statement-in-c-net), according to the C# specification "A goto statement cannot exit a finally block (§8.10). When a goto statement occurs within a finally block, the target of the goto statement must be within the same finally block, or otherwise a compile-time error occurs.". [Specification is here](https://msdn.microsoft.com/en-us/library/aa664758(v=vs.71).aspx), paragraph about this is near the bottom. – Equalsk Oct 31 '16 at 16:29
  • @Equalsk - I think the OP already knows that - as he has included that extract in the question - he seems to be asking why the specification says that - which really is not a suitable question for SO. – PaulF Oct 31 '16 at 16:35
  • If you moved the moo: label to just before the closing parenthesis of the finally block wouldn't that give the desired behaviour - assuming you did want to go to the next statement after the finally block. Still best not to use goto though. – PaulF Oct 31 '16 at 16:37
  • I've clearly got my glasses on upside down today, it's clearly written right there in a big block like you said. – Equalsk Oct 31 '16 at 16:38
  • 1
    @PanagiotisKanavos i was trying to see in both case i am trying to migrate from finally statement and in first case i get an error but second case works like charm. – Vishwa Ratna Oct 31 '16 at 16:39
  • "as it leads to unnecessary calls" What? No it doesn't. –  Jan 11 '19 at 13:39
  • 1
    @Amy The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided – Vishwa Ratna Jan 11 '19 at 13:54
  • @CommonMan I am quite aware of that, but it doesn't "lead to unnecessary calls". That's simply wrong. –  Jan 11 '19 at 14:00
  • 1
    @Amy ok, i was very new that time and was learning programming. Thanks for reminder, i edited it now. – Vishwa Ratna Jan 11 '19 at 14:02

1 Answers1

16

Isn't the goto moo and static void moo() doing the same act i.e taking me out of finally block?

No, absolutely not.

goto moo would transfer control out of the finally block completely.

moo() just calls the method, but then when the method returns you're back in the finally block.

If you put a Console.WriteLine("Still here...") after your moo() method you'll see that.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194