为何首次尝试时Polly异常未被捕获,抛出未处理异常?
Hey Danhol86, let's break down why you might be hitting this uncaught Polly exception on the first run—this is a frequent gotcha, so let's walk through the most probable culprits:
You didn't register the right exception type in your policy
Polly only handles the exact exceptions you explicitly tell it to. If your first execution throws an exception that's not included inHandle<ExceptionType>()(orHandleResult<T>()for result-based policies), Polly won't catch it—it'll just bubble up as an uncaught exception. For example:// This policy only handles HttpRequestException var retryPolicy = Policy.Handle<HttpRequestException>().Retry(3); // But your first call throws a TimeoutException—Polly ignores it retryPolicy.Execute(() => { throw new TimeoutException(); });The fix here is to either add the missing exception type to your policy or use a broader catch (like
Handle<Exception>()if that makes sense for your use case).Your code isn't actually wrapped by the policy
It's easy to accidentally run your target code outside the policy's execution scope. Double-check that you're usingpolicy.Execute(() => { /* your logic */ })(orawait policy.ExecuteAsync(...)for async code). If you call the method directly without wrapping it in the policy, Polly can't intercept any exceptions at all.Fallback policies are misconfigured or missing
If you're expecting a fallback to catch exceptions when the policy gives up, but you haven't linked it correctly to your retry/circuit breaker policy, the first exception might slip through. For example, if you create a retry policy and a fallback policy separately but don't combine them withPolicy.Wrap(fallbackPolicy, retryPolicy), the fallback won't activate—even on the first failed execution if no retries are set.Async/await mismatch is causing unobserved exceptions
If your target code is async, but you use Polly's synchronousExecute()method instead ofExecuteAsync(), Polly won't properly handle async exceptions. This leads to unobserved task exceptions, which can show up as uncaught Polly-related errors in your app. Always match your policy execution method to the async/sync nature of your code.Circuit breaker is stuck in an open state on startup
If you're using a circuit breaker policy and your app didn't shut down cleanly last time, the circuit might be left open. The first execution will trigger aBrokenCircuitException, which won't be handled if your policy doesn't explicitly account for it.
Quick Troubleshooting Hack
To get to the bottom of this fast, wrap your policy execution in a temporary try/catch to log the exact exception details:
try { await myPollyPolicy.ExecuteAsync(async () => { /* your code here */ }); } catch (Exception ex) { Console.WriteLine($"Caught exception: {ex.GetType().FullName} - {ex.Message}"); throw; // Re-throw to keep the original exception flow intact }
This will tell you exactly what exception is escaping Polly's handling, so you can adjust your policy to include it.
内容的提问来源于stack exchange,提问作者Danhol86

