Polly: Throw on final exception

Created on 14 Sep 2018  路  5Comments  路  Source: App-vNext/Polly

Hi,

Is it possible to throw the exception if the final Retry Fails? I am using the WaitAndRetryAsync, but I wish to throw the exception if the final retry throws and exception.

I can see the CircuitBreaker throws the exception but is it possible waiting before retries?

Thanks in advance!

question

Most helpful comment

Hi @grik001 . Happy to look at any code if you can post a repro. Here within Polly is a unit test demonstrating that WaitAndRetryAsync does rethrow the final exception.

Couple of things that could cause a final exception not to be rethrown:

  • .ExecuteAsync(...), like any async API, returns Task. If the result of .ExecuteAsync(...) isn't awaited, then the exception will remain captured on the Task rather than rethrown.
  • If using .ExecuteAndCaptureAsync(...), then the design intent of that is to capture any final exception into the FinalException property. .ExecuteAndCaptureAsync(...) captures any final exception, whereas .ExecuteAsync(...) rethrows it.

All 5 comments

Hi @grik001

Is it possible to throw the exception if the final Retry Fails?

That is exactly what the library does by default - see how retry works in the wiki.

Let me know if I've not understood the question, and you're after something slightly different!

I will test this again, but I am sure that WaitAndRetryAsync was blocking the exception even if the last try does not succeed.

Hi @grik001 . Happy to look at any code if you can post a repro. Here within Polly is a unit test demonstrating that WaitAndRetryAsync does rethrow the final exception.

Couple of things that could cause a final exception not to be rethrown:

  • .ExecuteAsync(...), like any async API, returns Task. If the result of .ExecuteAsync(...) isn't awaited, then the exception will remain captured on the Task rather than rethrown.
  • If using .ExecuteAndCaptureAsync(...), then the design intent of that is to capture any final exception into the FinalException property. .ExecuteAndCaptureAsync(...) captures any final exception, whereas .ExecuteAsync(...) rethrows it.

@reisenberger

Hi, I confirmed that you are right, sorry for wasting your time and thanks for the explanation

No problem @grik001 ! Let us know if there is anything else we can help with.

Was this page helpful?
0 / 5 - 0 ratings