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

时间:2012-02-14 05:10:04

标签: c# .net exception exception-handling

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

例如:

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

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

7 个答案:

答案 0 :(得分:36)

把它放在一个循环中。可能会在布尔标志周围循环一下,以控制何时最终要退出。

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

小心避免无限循环。

更好的方法可能是将“某些代码”放在自己的方法中,然后您可以在try和catch中调用该方法。

答案 1 :(得分:5)

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

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

或者你可以循环播放。

int retries = 0;

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

答案 2 :(得分:4)

[root@ONS88 ~]# yum install mysql mysql-server mysql-devel 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.s.uw.edu
 * epel: fedora.mirrors.pair.com
 * extras: centos.s.uw.edu
 * ius: mirror.its.dal.ca
 * nux-dextop: mirror.li.nux.ro
 * updates: centos.s.uw.edu
Resolving Dependencies
--> Running transaction check
---> Package mariadb101u.x86_64 1:10.1.29-1.ius.centos7 will be installed
--> Processing Dependency: mariadb101u-common(x86-64) = 1:10.1.29-1.ius.centos7 for package: 1:mariadb101u-10.1.29-1.ius.centos7.x86_64
--> Processing Dependency: mariadb101u-libs(x86-64) = 1:10.1.29-1.ius.centos7 for package: 1:mariadb101u-10.1.29-1.ius.centos7.x86_64
---> Package mariadb101u-devel.x86_64 1:10.1.29-1.ius.centos7 will be installed
---> Package mysql-community-server.x86_64 0:5.6.38-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.38-2.el7 for package: mysql-community-server-5.6.38-2.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.38-2.el7.x86_64
--> Running transaction check
---> Package mariadb101u-common.x86_64 1:10.1.29-1.ius.centos7 will be installed
--> Processing Dependency: mariadb101u-config(x86-64) = 1:10.1.29-1.ius.centos7 for package: 1:mariadb101u-common-10.1.29-1.ius.centos7.x86_64
---> Package mariadb101u-libs.x86_64 1:10.1.29-1.ius.centos7 will be installed
---> Package mysql-community-client.x86_64 0:5.6.38-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.38-2.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.38-2.el7 will be installed
--> Running transaction check
---> Package mariadb101u-config.x86_64 1:10.1.29-1.ius.centos7 will be installed
---> Package mysql-community-libs.x86_64 0:5.6.38-2.el7 will be installed
--> Processing Conflict: 1:mariadb101u-libs-10.1.29-1.ius.centos7.x86_64 conflicts mysql-community-libs
--> Processing Conflict: 1:mariadb101u-10.1.29-1.ius.centos7.x86_64 conflicts mysql-community-client
--> Processing Conflict: 1:mariadb101u-config-10.1.29-1.ius.centos7.x86_64 conflicts mysql-community-server
--> Processing Conflict: 1:mariadb101u-common-10.1.29-1.ius.centos7.x86_64 conflicts mysql-community-common
--> Finished Dependency Resolution
Error: mariadb101u-libs conflicts with mysql-community-libs-5.6.38-2.el7.x86_64
Error: mariadb101u conflicts with mysql-community-client-5.6.38-2.el7.x86_64
Error: mariadb101u-config conflicts with mysql-community-server-5.6.38-2.el7.x86_64
Error: mariadb101u-common conflicts with mysql-community-common-5.6.38-2.el7.x86_64
 You could try using --skip-broken to work around the problem
** Found 115 pre-existing rpmdb problem(s), 'yum check' output follows:
ModemManager-1.6.0-2.el7.x86_64 is a duplicate with ModemManager-1.1.0-8.git20130913.el7.x86_64
ModemManager-glib-1.6.0-2.el7.x86_64 is a duplicate with ModemManager-glib-1.1.0-8.git20130913.el7.x86_64
adcli-0.8.1-3.el7.x86_64 is a duplicate with adcli-0.7.5-4.el7.x86_64
alsa-tools-firmware-1.1.0-1.el7.x86_64 is a duplicate with alsa-tools-firmware-1.0.28-2.el7.x86_64
at-3.1.13-22.el7.x86_64 is a duplicate with at-3.1.13-20.el7.x86_64
avahi-0.6.31-17.el7.x86_64 is a duplicate with avahi-0.6.31-15.el7.x86_64
avahi-autoipd-0.6.31-15.el7.x86_64 has installed conflicts avahi > ('0', '0.6.31', '15.el7'): avahi-0.6.31-17.el7.x86_64
avahi-autoipd-0.6.31-17.el7.x86_64 has installed conflicts avahi < ('0', '0.6.31', '17.el7'): avahi-0.6.31-15.el7.x86_64

答案 3 :(得分:3)

答案 4 :(得分:1)

还有另一种方法可以做到(虽然正如其他人提到的那样,并不是真的推荐)。下面是一个使用文件下载重试的示例,以更紧密地匹配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;
}

答案 5 :(得分:1)

ole goto有什么问题?

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

                Thread.Sleep(1000);
                goto Start;
            }

答案 6 :(得分:0)

这应该有效:

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