移到代码中的前一个给定行

Nal*_*526 -1 c#

如果发生微粒异常,我需要运行相同的代码.所以我尝试使用goto,但goto声明我不能在goto语句定位之前移动到一行

示例代码,

try
{
    top:

    //Code

}
catch (Exception ex)
{
    if (ex.Message.Substring(1, 5) == "error")
    {
        goto top: //Error - Can not resolve symbol `top`
        goto bottom: //OK
    }
}

bottom:
    //Code
}
Run Code Online (Sandbox Code Playgroud)

如何执行上一行代码?

Sri*_*vel 6

您的代码可以轻松地重写为以下内容.

while (true)
{
    try
    {
          //Code
    }
    catch (Exception ex)
    {
        if (ex.Message.Substring(1, 5) == "error")
        {
            continue;
            //goto bottom; //This doesn't makes sense after we transfer control 
        }
        else
        {
             break;//Did you mean this?
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

正如评论中所指出的,使用一些计数器来防止在出现故障时连续循环是一个好主意.