Sometimes an exception isn’t just an exception. For example, SQL can have connection or timeout issues and file IO can have problems competing for file access. In these situations, sometimes it makes sense to try executing the code again rather than letting it completely error out. Below is some C# sample code for executing some simple retry logic.
for (int retryAttempt = 1; retryAttempt <= MAX_RETRY_ATTEMPTS; retryAttempt++) { try { // Perform code execution here break; } catch (Exception ex) { // Perform any logging here if (retryAttempt < MAX_RETRY_ATTEMPTS) { Thread.Sleep(1000); // Sleep 1 second before retrying } else { throw; } } }
As you can see, it’s pretty simple. Execute the actual code logic within the try block, and perform any necessary exception logging within the catch block.
Richard Franzen
Sr. Developer
ImageSource, Inc.