I'm, trying to check a simple WaitAndRetry of Polly
class Program
{
public static void Main()
{
int i = 0;
var _retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetry(Backoff.ExponentialBackoff(TimeSpan.FromSeconds(2), 10),
(exception, timespan) =>
{
Console.WriteLine($"Retry: {timespan}. \n ex: {exception}");
});
_retryPolicy.Execute(() =>
{
Console.WriteLine(i);
i ;
int.Parse("something");
});
Console.ReadLine();
}
}
And I want to throw a final exception after all the retries are failed. How can I do it?
Excepted Result:
Retry: ..
Retry: ..
Retry: ..
My new final error!
Thank You!
CodePudding user response:
You don't have to explicitly re-throw the last exception.
The retry works in the following way:
- If an exception is thrown by the decorated method and there is a related
.Handle<TEx>
or.Or<TEx>
clause inside the policy definition then it checks if the retry limit has been reached or not- If the retry limit has not been exceeded then it will perform yet another retry attempt
- If the retry limit has been reached then it will throw the last exception
- If there is not related
.Handle<TEx>
or.Or<TEx>
clause then it will throw the last exception
Please also note that if you run your application in a debug mode then the IDE might stop each time when the decorated method throws exception. It depends on your IDE settings.