C# 在 catch 块中捕获异常后,是否可以再次执行 try 块中的代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9272016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:39:34  来源:igfitidea点击:

Is it possible to execute the code in the try block again after an exception in caught in catch block?

c#.netexceptionexception-handling

提问by Infant Dev

I want to execute the code in the try block again after an exception is caught. Is that possible somehow?

我想在捕获异常后再次执行 try 块中的代码。这有可能吗?

For Eg:

例如:

try
{
    //execute some code
}
catch(Exception e)
{
}

If the exception is caught I want to go in the try block again to "execute some code" and try again to execute it.

如果异常被捕获,我想再次进入 try 块以“执行一些代码”并再次尝试执行它。

采纳答案by ziesemer

Put it in a loop. Possibly a while loop around a boolean flag to control when you finally want to exit.

把它放在一个循环中。可能会围绕布尔标志进行 while 循环,以控制您何时最终想要退出。

bool tryAgain = true;
while(tryAgain){
  try{
    // execute some code;
    // Maybe set tryAgain = false;
  }catch(Exception e){
    // Or maybe set tryAgain = false; here, depending upon the exception, or saved details from within the try.
  }
}

Just be careful to avoid an infinite loop.

只要小心避免无限循环。

A better approach may be to put your "some code" within its own method, then you could call the method from both within the try and the catch as appropriate.

更好的方法可能是将您的“一些代码”放在它自己的方法中,然后您可以根据需要从 try 和 catch 中调用该方法。

回答by Richard Rhyan

This should work:

这应该有效:

count = 0;
while (!done) {
  try{
    //execute some code;
    done = true;
  }
  catch(Exception e){
  // code
  count++;
  if (count > 1) { done = true; }
  }
}

回答by SynXsiS

If you wrap your block in a method, you can recursively call it

如果将块包装在方法中,则可以递归调用它

void MyMethod(type arg1, type arg2, int retryNumber = 0)
{
    try
    {
        ...
    }
    catch(Exception e)
    {
        if (retryNumber < maxRetryNumber)
            MyMethod(arg1, arg2, retryNumber+1)
        else
            throw;
    }
}

or you could do it in a loop.

或者你可以循环进行。

int retries = 0;

while(true)
{
    try
    {
        ...
        break; // exit the loop if code completes
    }
    catch(Exception e)
    {
        if (retries < maxRetries)
            retries++;
        else
            throw;
    }
}

回答by Bill

There is another way to do it (though as others have mentioned, not really recommended). Here's an example using a file download retry to more closely match the retrykeyword found in Ruby in VB6.

还有另一种方法可以做到(尽管正如其他人所提到的,并不是真正推荐的)。下面是一个示例,该示例使用文件下载重试来更紧密地匹配retry在 VB6 中的 Ruby 中找到的关键字。

RetryLabel:

try
{
    downloadMgr.DownLoadFile("file:///server/file", "c:\file");
    Console.WriteLine("File successfully downloaded");
}
catch (NetworkException ex)
{
    if (ex.OkToRetry)
        goto RetryLabel;
}

回答by LastTribunal

What's wrong with the ole goto?

olegoto怎么了?

 Start:
            try
            {
                //try this
            }
            catch (Exception)
            {

                Thread.Sleep(1000);
                goto Start;
            }

回答by David Smith

int tryTimes = 0;
while (tryTimes < 2) // set retry times you want
{
    try
    {
        // do something with your retry code
        break; // if working properly, break here.
    }
    catch
    {
        // do nothing and just retry
    }
    finally
    {
        tryTimes++; // ensure whether exception or not, retry time++ here
    }
}