它是唯一一种指向无法访问的标签的可访问goto吗?

时间:2013-03-04 23:28:01

标签: c# unreachable-code

灵感来自问题

在我提出这个问题之前,我读过:

问题中的挑战是

  

编写一个程序,该程序具有可访问的goto语句但相应的标记语句无法访问 - Eric Lippert

和一个可行的答案就像是

    // 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();

我想知道在单个线程程序中,是唯一的情况指向无法访问的标签的可达goto吗?以下代码也被认为是可行的答案吗?

here:
    int d=0, n=1/d;
    goto here;

1 个答案:

答案 0 :(得分:5)

finally - 阻止goto技巧实际上是获取针对无法访问标签的可访问goto的唯一方法。