Is it possible to handle different exceptions differently with the same policy?
See original GitHub issueI 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:
- Created 7 years ago
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 toonRetry
delegate before the next try commences, so you can varyonRetry
actions depending on the exception causing the retry. So:Does this cover it? Let us know if you have any other questions!
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:
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:
or (an alternative syntax under consideration):
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?