避免转到的最佳方法是什么?

vis*_*213 0 c# goto

我经常陷入goto似乎是我心中最好的选择的情况.但我已多次阅读不使用它,并且总有一种替代方案.现在,我正在尝试这样的事情: -

    try{
            //Something that requires internet connectivity;
    }
     catch{
            //Show a message-Internet connectivity lost,and go back to try
    //-->FYI--Ignore "show message", because I am just appending this text to a  
    // textbox. So there won't be a problem of multiple ShowMessage Boxes.
      }
Run Code Online (Sandbox Code Playgroud)

现在,我认为最好的选择是在catch语句中使用goto,但我试图避免它.try是函数中的第一个语句,如果我记得那个函数,我正在堆积堆栈,所以这不是更好的选择.我可以选择什么?

Jam*_*mes 8

使用while带有标志的循环

var tryAgain = true;
while (tryAgain) 
{
    try
    {
        ...
        tryAgain = false;
    }
    catch (...)
    {
        tryAgain = ...
    }
}
Run Code Online (Sandbox Code Playgroud)