HI!
I have code like this:
try
{
DoSomething();
} catch (Exception) {}
How to do it with Polly?
It means I want to try to execute some method and not handle any exception when it fails - just keep running.
Hi @moabtools . If I have understood correctly, you want to swallow exceptions and just keep running, using Polly.
You could create a FallbackPolicy which does nothing. The below example is for a synchronous, void-returning execution (per the example in your question):
var swallowExceptions = Policy.Handle<Exception>().Fallback(() => { });
swallowExceptions.Execute(() => DoSomething());
For TResult-returning executions, an approach could be similar, but the FallbackPolicy would have to specify a substitute TResult value.
Hi, @reisenberger. Thanks for reply, but that's not working as expected:
static void Main(string[] args)
{
var swallowExceptions = Policy.Handle<Exception>().Fallback(() => { });
swallowExceptions.Execute(() => DoSomething());
Console.ReadLine();
}
static void DoSomething()
{
throw new Exception(); // VS falls here
}

But I dont want to handle exceptions at all.
@moabtools If you run your app without the debugger you will that see it is working perfectly.
All you are seeing with the debugger, is the debugger breaking when and where the exception is first thrown, to show it to you. When that happens in the debugger, if you press "continue", you will then see the execution proceed, and you will see that the policy does indeed swallow the exception.
See this stack overflow question and our detailed wiki article on debugging with Polly, for more explanation.
Thats very strange and not transparent. Why not to realize some method like Policy.Handle<Exception>().NotThrow() without that debugger dances? This can be very useful in such cases.
Why not to realize some method like Policy.Handle
().NotThrow() without that debugger dances?
Because it is not possible. As the article and stackoverflow question I linked explain, this is entirely a debugger artefact. _The way the debugger behaves here is not something that any code within Polly could change._ But we do provide guidance on how to improve your debugging experience. The wiki article I linked explains in detail how to stop these debugger dances, if you don't like them.
Thank you for answers! I will dive into this article.
@moabtools No problem! Closing as think we have answered the questions, but post or open a new issue if you need anything else.
Most helpful comment
Because it is not possible. As the article and stackoverflow question I linked explain, this is entirely a debugger artefact. _The way the debugger behaves here is not something that any code within Polly could change._ But we do provide guidance on how to improve your debugging experience. The wiki article I linked explains in detail how to stop these debugger dances, if you don't like them.