question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to handle different exceptions differently with the same policy?

See original GitHub issue

I just started digging into Polly and I really like it. One thing I can’t quite seem to figure out how to do is to have a Policy that reacts differently to different exception types.

I’d like to do something like this:

// don't need to reconnect on a CommandException
myPolicy = Policy
.Handle<CommandException>()               
.Retry(10);

// but need to reconnect before a retry when an IOException occurs
myPolicy.Handle<IOException>()
.Retry(10, (exception, retryCount, context) => 
{    
    Reconnect();
});

myPolicy.Execute(Command);

Is this possible?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

21reactions
reisenbergercommented, May 8, 2016

Hi @BertLamb Using .Or<TException> lets you handle more than one type of exception in the same policy. And, the exception just thrown is passed the to onRetry delegate before the next try commences, so you can vary onRetry actions depending on the exception causing the retry. So:

// don't need to reconnect on a CommandException
// but need to reconnect before a retry when an IOException occurs
myPolicy = Policy
    .Handle<CommandException>()
    .Or<IOException>()               
    .Retry(10, (exception, retryCount, context) => 
    {    
        if (exception is IOException) { Reconnect(); }
    });

myPolicy.Execute(Command);

Does this cover it? Let us know if you have any other questions!

11reactions
reisenbergercommented, May 9, 2016

Hi @BertLamb . To do that with Polly, you can define separate policies and nest them, as described in the wiki here or as shown below:

var retryPolicy = Policy
    .Handle<CommandException>()
    .Retry(10, (exception, retryCount, context) => Reconnect());
var circuitBreaker = Policy
    .Handle<IOException>
    .CircuitBreaker(2, TimeSpan.FromMinutes(1));

retryPolicy.Execute(() => circuitBreaker.Execute(command));

There isn’t currently a way to define a Policy that handles a variety of different exceptions in a variety of different ways, all in one single fluent statement. However, the Polly Roadmap envisages the Polly Pipeline, which would allow any number of functionally-composed policies to be reduced to one Policy, thus:

var combinedPolicy = Policy.Pipeline(retryPolicy, circuitBreaker);
combinedPolicy.Execute(command);

or (an alternative syntax under consideration):

var combinedPolicy = retryPolicy.Wrap(circuitBreaker);
combinedPolicy.Execute(command);

I guess once the functionality for collapsing functionally-composed (wrapped) policies into one (as in the Polly Pipeline) was in place, it might be possible to create an on-going fluent syntax as follows - is this the kind of thing you had in mind?

var myPolicy = Policy
    .Handle<CommandException>()
    .Retry(10, (exception, retryCount, context) => Reconnect());
    .Handle<IOException>
    .CircuitBreaker(2, TimeSpan.FromMinutes(1));

myPolicy.Execute(command);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to catch multiple exceptions and perform ...
Yes, it is possible to catch multiple Exception in same catch block and perform different logic according to Exception caught. For Example:
Read more >
Handling same exception thrown by different methods
Handling same exception thrown by different methods ... The exception may or may not be caused by the same event internal to the...
Read more >
How to Catch Multiple Exceptions in Python
By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used ...
Read more >
Java Catch Multiple Exceptions, Rethrow Exception
If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, exception parameter (ex) is...
Read more >
Multiple Exception Handling in Python
Although it's not specific to handle multiple exceptions per se, it is worth noting that one can get a handle to the thrown...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found