dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidCtorParameterNoInfinity, "value")

Error message

SR.Format(SR.InvalidCtorParameterNoInfinity, "value")

What it means

The GridLength(double value, GridUnitType type) constructor only accepts finite numbers for pixel/star sizes. When double.IsInfinity(value) is true it throws ArgumentException (SR.InvalidCtorParameterNoInfinity) because a GridLength must represent a real layout size; NaN is rejected separately just above this check.

Solutions

  1. Validate the value is finite before constructing: if (!double.IsFinite(value)) throw/repair.
  2. Use GridUnitType.Auto for content-driven sizing instead of an Infinity sentinel.
  3. Trace where Infinity originates (usually a divide-by-zero or unbounded double accumulation) and fix the upstream math.
  4. Clamp the value to double.MaxValue or a sensible maximum before constructing.

Example fix

// before
double w = totalWidth / columnCount; // Infinity when columnCount == 0
var length = new GridLength(w, GridUnitType.Pixel);

// after
double w = columnCount > 0 ? totalWidth / columnCount : 0;
if (!double.IsFinite(w)) w = 0;
var length = new GridLength(w, GridUnitType.Pixel);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(value) || double.IsInfinity(value))
    throw new ArgumentException(nameof(value) + " must be a finite number");
var length = new GridLength(value, GridUnitType.Pixel);

Type guard

bool IsValidGridLengthValue(double v) => double.IsFinite(v);

Try / catch

try { var len = new GridLength(value, unit); }
catch (ArgumentException ex) when (ex.Message.Contains("Infinity") || ex.Message.Contains("NaN"))
{ /* fall back to Auto */ len = GridLength.Auto; }

Prevention

When it happens

Trigger: Calling new GridLength(double.PositiveInfinity, GridUnitType.Pixel), new GridLength(double.NegativeInfinity, GridUnitType.Star), or any ctor overload whose value became Infinity through arithmetic (division by zero, overflow of double.MaxValue sums) before construction.

Common situations: Binding a column/row size to a computed value where the divisor was 0; copying sizes from APIs that return Infinity for 'unbounded'; porting code that used Auto-style Infinity sentinels from other UI frameworks.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cb2e226c33c49480. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs:96

        /// instance.</param>
        /// <remarks> 
        /// If the <c>type</c> parameter is <c>GridUnitType.Auto</c>, 
        /// then passed in value is ignored and replaced with <c>0</c>.
        /// </remarks>
        /// <exception cref="ArgumentException">
        /// If <c>value</c> parameter is <c>double.NaN</c>
        /// or <c>value</c> parameter is <c>double.NegativeInfinity</c>
        /// or <c>value</c> parameter is <c>double.PositiveInfinity</c>.
        /// </exception>
        public GridLength(double value, GridUnitType type)
        {
            if (double.IsNaN(value))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, "value"));
            }
            if (double.IsInfinity(value))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoInfinity, "value"));
            }
            if (    type != GridUnitType.Auto
                &&  type != GridUnitType.Pixel
                &&  type != GridUnitType.Star   )
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, "type"));
            }

            _unitValue = (type == GridUnitType.Auto) ? 0.0 : value;
            _unitType = type;
        }

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Public Methods
        //

View on GitHub (pinned to 81131a70a4)