Danhol86
Danhol86

Reputation: 1472

Polly exception not being caught

Can someone please explain why the below code is failing on first attempt and throwing unhandled exception? Much appreciated.

using Polly;
using System;
using System.Threading;

namespace TestPolly
{
    class Program
    {
    static void Main(string[] args)
    {
        Policy.Handle<DivideByZeroException>().Retry(10).Execute(() => DoSomething(0));
    }

    private static void DoSomething(int num)
    {
        Thread.Sleep(1000);
        Console.WriteLine("Doing division");
        var y = 2 / num;
    }
}

Example

Upvotes: 3

Views: 2127

Answers (1)

mountain traveller
mountain traveller

Reputation: 8156

You are just seeing the debugger break on the Exception, as explained in detail in this article on the Polly wiki.

Can someone please explain why the below code is failing on first attempt and throwing unhandled exception?

The code is not failing nor throwing an unhandled exception. When you press F5 (or click Continue in the debugger) to continue debugging, you will see execution continue - and that the exception is being handled by the policy.

Upvotes: 5

Related Questions