简体   繁体   English

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

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

I want to execute the code in the try block again after an exception is caught. 我想在捕获异常后再次执行try块中的代码。 Is that possible somehow? 这有可能吗?

For Eg: 对于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块以“执行一些代码”并再次尝试执行它。

Put it in a loop. 把它放在一个循环中。 Possibly a while loop around a boolean flag to control when you finally want to exit. 可能会在布尔标志周围循环一圈,以控制何时最终要退出。

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中调用该方法。

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;
    }
}
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
    }
}

What's wrong with the ole goto ? ole goto什么问题?

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

                Thread.Sleep(1000);
                goto Start;
            }

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 retry keyword found in Ruby in VB6. 下面是一个使用文件下载重试的示例,以更紧密地匹配VB6中Ruby中的retry关键字。

RetryLabel:

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

This should work: 这应该工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM