{"record":{"id":"eb0b473f20549143","repo":"App-vNext/Polly","slug":"value-must-be-greater-than-or-equal-to-zero-eb0b47","errorCode":null,"errorMessage":"Value must be greater than or equal to zero.","messagePattern":"Value must be greater than or equal to zero\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Polly/Retry/RetrySyntax.cs","lineNumber":50,"sourceCode":"    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>\n    public static RetryPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry) =>\n        policyBuilder.Retry(1, onRetry);\n\n    /// <summary>\n    /// Builds a <see cref=\"Policy\"/> that will retry <paramref name=\"retryCount\"/> times\n    /// calling <paramref name=\"onRetry\"/> on each retry with the raised exception and retry count.\n    /// </summary>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"retryCount\">The retry count.</param>\n    /// <param name=\"onRetry\">The action to call on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentOutOfRangeException\">retryCount;Value must be greater than or equal to zero.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>\n    public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry)\n    {\n        if (retryCount < 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(retryCount), \"Value must be greater than or equal to zero.\");\n        }\n\n        if (onRetry == null)\n        {\n            throw new ArgumentNullException(nameof(onRetry));\n        }\n\n        return policyBuilder.Retry(retryCount, (outcome, i, _) => onRetry(outcome, i));\n    }\n\n    /// <summary>\n    /// Builds a <see cref=\"Policy\"/> that will retry once\n    /// calling <paramref name=\"onRetry\"/> on retry with the raised exception, retry count and context data.\n    /// </summary>\n    /// <param name=\"policyBuilder\">The policy builder.</param>\n    /// <param name=\"onRetry\">The action to call on each retry.</param>\n    /// <returns>The policy instance.</returns>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"onRetry\"/> is <see langword=\"null\"/>.</exception>","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/RetrySyntax.cs#L32-L68","documentation":"Thrown by the legacy Polly v7 synchronous Retry API (PolicyBuilder, exception-based) when retryCount is negative for the overload taking Action<Exception, int> onRetry. Polly treats a negative retry count as invalid because the count bounds how many times the delegate re-executes after a handled failure.","triggerScenarios":"Calling Policy.Handle<...>().Retry(retryCount, onRetry) with retryCount < 0 at RetrySyntax.cs:50.","commonSituations":"Reading retry count from appsettings where an unset value is read as -1; computing count from an arithmetic expression that underflows; passing a raw config int without bounds checking.","solutions":["Validate retryCount >= 0 before calling Retry; use 0 to run without retries.","Correct the configuration so it cannot return a negative number.","Add a startup guard that fails the app loudly on bad config instead of letting it reach Polly."],"exampleFix":"// before\nvar policy = Policy.Handle<HttpRequestException>().Retry(_config.RetryCount, (ex, i) => Log(ex, i));\n// after\nif (_config.RetryCount < 0) throw new InvalidOperationException(\"RetryCount must be >= 0\");\nvar policy = Policy.Handle<HttpRequestException>().Retry(_config.RetryCount, (ex, i) => Log(ex, i));","handlingStrategy":"validation","validationCode":"if (retryCount < 0)\n    throw new ArgumentOutOfRangeException(nameof(retryCount), \"Retry count must be >= 0.\");\nvar policy = Policy.Handle<TException>(pred).Retry(retryCount, onRetry);","typeGuard":"static bool IsValidRetryCount(int count) => count >= 0;","tryCatchPattern":null,"preventionTips":["Validate retry count from configuration at startup.","Use 0 to mean 'no retries' rather than negative sentinels.","Clamp derived counts with Math.Max(0, value)."],"tags":["argumentoutofrangeexception","polly","retry","sync","validation","csharp","legacy-api"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}