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

后端 未结 7 833
灰色年华
灰色年华 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:35

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

提交回复
热议问题