{"record":{"id":"cb2e226c33c49480","repo":"dotnet/wpf","slug":"sr-format-sr-invalidctorparameternoinfinity-value-gridlength","errorCode":null,"errorMessage":"SR.Format(SR.InvalidCtorParameterNoInfinity, \"value\")","messagePattern":"SR\\.Format\\(SR\\.InvalidCtorParameterNoInfinity, \"value\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs","lineNumber":96,"sourceCode":"        /// instance.</param>\n        /// <remarks> \n        /// If the <c>type</c> parameter is <c>GridUnitType.Auto</c>, \n        /// then passed in value is ignored and replaced with <c>0</c>.\n        /// </remarks>\n        /// <exception cref=\"ArgumentException\">\n        /// If <c>value</c> parameter is <c>double.NaN</c>\n        /// or <c>value</c> parameter is <c>double.NegativeInfinity</c>\n        /// or <c>value</c> parameter is <c>double.PositiveInfinity</c>.\n        /// </exception>\n        public GridLength(double value, GridUnitType type)\n        {\n            if (double.IsNaN(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, \"value\"));\n            }\n            if (double.IsInfinity(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoInfinity, \"value\"));\n            }\n            if (    type != GridUnitType.Auto\n                &&  type != GridUnitType.Pixel\n                &&  type != GridUnitType.Star   )\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, \"type\"));\n            }\n\n            _unitValue = (type == GridUnitType.Auto) ? 0.0 : value;\n            _unitType = type;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs#L78-L114","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the value is finite before constructing: if (!double.IsFinite(value)) throw/repair.","Use GridUnitType.Auto for content-driven sizing instead of an Infinity sentinel.","Trace where Infinity originates (usually a divide-by-zero or unbounded double accumulation) and fix the upstream math.","Clamp the value to double.MaxValue or a sensible maximum before constructing."],"exampleFix":"// before\ndouble w = totalWidth / columnCount; // Infinity when columnCount == 0\nvar length = new GridLength(w, GridUnitType.Pixel);\n\n// after\ndouble w = columnCount > 0 ? totalWidth / columnCount : 0;\nif (!double.IsFinite(w)) w = 0;\nvar length = new GridLength(w, GridUnitType.Pixel);","handlingStrategy":"validation","validationCode":"if (double.IsNaN(value) || double.IsInfinity(value))\n    throw new ArgumentException(nameof(value) + \" must be a finite number\");\nvar length = new GridLength(value, GridUnitType.Pixel);","typeGuard":"bool IsValidGridLengthValue(double v) => double.IsFinite(v);","tryCatchPattern":"try { var len = new GridLength(value, unit); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Infinity\") || ex.Message.Contains(\"NaN\"))\n{ /* fall back to Auto */ len = GridLength.Auto; }","preventionTips":["Always run values through double.IsFinite before constructing GridLength.","Never use Infinity as an 'auto' sentinel; use GridUnitType.Auto.","Check for divide-by-zero in any width/height math feeding GridLength."],"tags":["wpf","gridlength","argumentexception","infinity","constructor"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}