문제

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;
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top