Domanda

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;
È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top