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

后端 未结 7 832
灰色年华
灰色年华 2021-02-05 06:18

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

For Eg:

try
{
    //execute some code
}
catch(E         


        
7条回答
  •  生来不讨喜
    2021-02-05 06:36

    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.

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

提交回复
热议问题