1

Inspired from question

And before I ask this question, I've read:

The challenge in the question is

write a program which has a reachable goto statement but the corresponding labelled statement is unreachable - Eric Lippert

and one feasible answer is like

    // the 3 lines are not important but declare variable for afterwards use
    var whateverException=new Exception("whatever exception");
    var whateverAction=default(Action);
    whateverAction=() => whateverAction();
    try {
        goto whateverLabel; // (1) the goto is reachable
    }
    finally {
        throw whateverException; // (3) because finally hijacks
    }

whateverLabel: // (2) but the label is not really reached
    whateverAction();

I'm wondering that in a single thread program, is it the only case a reachable goto pointing to an unreachable label? And is following code also considered a feasible answer of that?

here:
    int d=0, n=1/d;
    goto here;
Community
  • 1
  • 1
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • A question without any vote, answer, or even a comment for four hours is really weird .. – Ken Kin Mar 05 '13 at 03:43
  • 2
    The C# specification would consider your `goto here;` *reachable* because reachability only considers values of *constants*. `1/d` is not a constant. – Eric Lippert Mar 05 '13 at 06:45

1 Answers1

5

The finally-blocked goto trick is in fact the only way to get a reachable goto that targets an unreachable label.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067