{"record":{"id":"81105684af6045b2","repo":"App-vNext/Polly","slug":"value-must-be-greater-than-or-equal-to-zero-811056","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/RetryTResultSyntax.cs","lineNumber":54,"sourceCode":"    public static RetryPolicy<TResult> Retry<TResult>(this PolicyBuilder<TResult> policyBuilder, Action<DelegateResult<TResult>, int> onRetry) =>\n        policyBuilder.Retry(1, onRetry);\n\n    /// <summary>\n    /// Builds a <see cref=\"Policy{TResult}\"/> that will retry <paramref name=\"retryCount\"/> times\n    /// calling <paramref name=\"onRetry\"/> on each retry with the handled exception or result and retry count.\n    /// </summary>\n    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\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<TResult> Retry<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, Action<DelegateResult<TResult>, 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{TResult}\"/> that will retry once\n    /// calling <paramref name=\"onRetry\"/> on retry with the handled exception or result, retry count and context data.\n    /// </summary>\n    /// <typeparam name=\"TResult\">The type of the result.</typeparam>\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>","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/Retry/RetryTResultSyntax.cs#L36-L72","documentation":"This generic Retry<TResult> overload (RetryTResultSyntax.cs:54) throws ArgumentOutOfRangeException synchronously when retryCount is negative. The guard `retryCount < 0` enforces a valid attempt budget for the result-typed retry policy at construction time.","triggerScenarios":"Calling Policy.HandleResult<T>(...).Retry(retryCount, onRetry) where retryCount is negative, with onRetry typed Action<DelegateResult<TResult>, int>.","commonSituations":"Computed retry count that underflows (e.g. configured - 1 where configured is 0); a config value defaulting to -1; sentinel misuse.","solutions":["Pass retryCount >= 0 (0 means no retries).","Clamp config-sourced values with Math.Max(0, value).","Trace where the negative value originates."],"exampleFix":"// before\nvar policy = Policy.HandleResult<int>(r => r < 0).Retry(-1, onRetry);\n// after\nvar retryCount = Math.Max(0, configuredRetries);\nvar policy = Policy.HandleResult<int>(r => r < 0).Retry(retryCount, onRetry);","handlingStrategy":"validation","validationCode":"if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount));\nvar clamped = Math.Max(0, retryCount);\nvar policy = Policy.HandleResult<int>(r => r < 0).Retry(clamped, onRetry);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp config-sourced retry counts with Math.Max(0, value).","Reject negative retry counts at config-bind time.","Unit-test policy construction with boundary retry counts (0, 1, large)."],"tags":["polly","retry","argument-validation","generic","configuration","csharp"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}