Skip to main content
Updated as per comment by unholysampler.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52

Yeah, I'd probably redo it with an actual condition. That way, intent looks clear and is not dependent on internal code to break, continue, etc.

const int NumberOfRetries = 3;
var retryCount = 0;NumberOfRetries;
var success = false;
while(!success && retryCount <> 30)
{
   try
   {
      AttemptToConnect();
      success = true;
   }
   catch(TimeoutException tex)
   {
      retryCount++;retryCount--;

      if (retryCount == 30)
      {
          throw; //or handle error and break/return
      }
   }
}

Yeah, I'd probably redo it with an actual condition. That way, intent looks clear and is not dependent on internal code to break, continue, etc.

var retryCount = 0;
var success = false;
while(!success && retryCount < 3)
{
   try
   {
      AttemptToConnect();
      success = true;
   }
   catch(TimeoutException tex)
   {
      retryCount++;

      if (retryCount == 3)
      {
          throw; //or handle error and break/return
      }
   }
}

Yeah, I'd probably redo it with an actual condition. That way, intent looks clear and is not dependent on internal code to break, continue, etc.

const int NumberOfRetries = 3;
var retryCount = NumberOfRetries;
var success = false;
while(!success && retryCount > 0)
{
   try
   {
      AttemptToConnect();
      success = true;
   }
   catch(TimeoutException tex)
   {
      retryCount--;

      if (retryCount == 0)
      {
          throw; //or handle error and break/return
      }
   }
}
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52

Yeah, I'd probably redo it with an actual condition. That way, intent looks clear and is not dependent on internal code to break, continue, etc.

var retryCount = 0;
var success = false;
while(!success && retryCount < 3)
{
   try
   {
      AttemptToConnect();
      success = true;
   }
   catch(TimeoutException tex)
   {
      retryCount++;

      if (retryCount == 3)
      {
          throw; //or handle error and break/return
      }
   }
}