Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polly exception not being caught

Tags:

c#

.net

polly

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

like image 821
Danhol86 Avatar asked Jul 03 '26 20:07

Danhol86


1 Answers

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.

like image 51
mountain traveller Avatar answered Jul 06 '26 08:07

mountain traveller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!