dotnet/wpf · error · FormatException

SR.Format(SR.InvalidStringThickness, s)

Error message

SR.Format(SR.InvalidStringThickness, s)

What it means

FromString parses a delimited string of up to four doubles into Thickness components. If the tokenized string contains more than four numeric tokens, it throws FormatException with InvalidStringThickness including the offending string.

Solutions

  1. Correct the input string to 1, 2, or 4 space/comma separated numbers.
  2. Validate token count (split on ',' / whitespace) before converting.
  3. Catch FormatException around ConvertFrom/Parse and surface a user-facing validation message.

Example fix

// before
var t = (Thickness)converter.ConvertFrom("1,2,3,4,5"); // throws
// after
var t = (Thickness)converter.ConvertFrom("1,2,3,4");
Defensive patterns

Strategy: validation

Validate before calling

var tokens = s.Split(new[]{' ', ','}, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length > 4)
    throw new FormatException($"Thickness string must have 1, 2, or 4 numbers: '{s}'");

Try / catch

try { return (Thickness)converter.ConvertFrom(s); }
catch (FormatException ex) when (ex.Message.Contains(s)) { return defaultValue; }

Prevention

When it happens

Trigger: Calling ThicknessConverter.ConvertFrom (or Thickness.Parse) with a string like "1,2,3,4,5" — five or more comma/space separated numbers.

Common situations: User-entered or config-file thickness strings with too many values; concatenation bugs building the string; pasting values with duplicated separators producing extra tokens.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs:196

        /// <summary>
        /// Constructs a <see cref="Thickness"/> struct out of string representation supplied by <paramref name="s"/> and the specified <paramref name="cultureInfo"/>.
        /// </summary>
        /// <param name="s">The string representation of a <see cref="Thickness"/> struct.</param>
        /// <param name="cultureInfo">The <see cref="CultureInfo"/> which was used to format this string.</param>
        /// <returns>A new instance of <see cref="Thickness"/> struct representing the data contained in <paramref name="s"/>.</returns>
        /// <exception cref="FormatException">Thrown when <paramref name="s"/> contains invalid string representation.</exception>
        internal static Thickness FromString(string s, CultureInfo cultureInfo)
        {
            TokenizerHelper th = new(s, cultureInfo);
            Span<double> lengths = stackalloc double[4];
            int i = 0;

            // Peel off each double in the delimited list.
            while (th.NextToken())
            {
                if (i >= 4) // In case we've got more than 4 doubles, we throw
                    throw new FormatException(SR.Format(SR.InvalidStringThickness, s));

                lengths[i] = LengthConverter.FromString(th.GetCurrentToken(), cultureInfo);
                i++;
            }

            // We have a reasonable interpretation for one value (all four edges),
            // two values (horizontal, vertical),
            // and four values (left, top, right, bottom).
            return i switch
            {
                1 => new Thickness(lengths[0]),
                2 => new Thickness(lengths[0], lengths[1], lengths[0], lengths[1]),
                4 => new Thickness(lengths[0], lengths[1], lengths[2], lengths[3]),
                _ => throw new FormatException(SR.Format(SR.InvalidStringThickness, s)),
            };
        }

    #endregion

View on GitHub (pinned to 81131a70a4)