iOfficeAI/OfficeCLI · error · ArgumentException

Invalid '{context}' value '{s}'. Expected a finite number wi

Error message

Invalid '{context}' value '{s}'. Expected a finite number with optional unit (e.g. '12pt', '1.5x', '150%').

What it means

Thrown by SpacingConverter.ParseNumberAllowNegative when the numeric portion of a spacing/lineSpacing string is not a finite double: double.TryParse fails (non-numeric/garbage), or the result is NaN or Infinity. This is the bottom-of-stack parser every unit branch relies on, so any unparseable value bubbles here.

Source

Thrown at src/officecli/Core/SpacingConverter.cs:417

    {
        var result = ParseNumberAllowNegative(s, context);
        if (result < 0)
            throw new ArgumentException(
                $"Invalid '{context}' value '{s}'. Spacing values must be non-negative.");
        return result;
    }

    /// <summary>
    /// Parse a finite number without the non-negative gate. Used by the
    /// Auto-rule lineSpacing paths, whose target attribute (w:line) is
    /// ST_SignedTwipsMeasure and legitimately carries negatives in real docs.
    /// </summary>
    private static double ParseNumberAllowNegative(string s, string context)
    {
        var trimmed = s.Trim();
        if (!double.TryParse(trimmed, CultureInfo.InvariantCulture, out var result)
            || double.IsNaN(result) || double.IsInfinity(result))
            throw new ArgumentException(
                $"Invalid '{context}' value '{s}'. Expected a finite number with optional unit (e.g. '12pt', '1.5x', '150%').");
        return result;
    }
}

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a supported format: a finite number with one of pt, cm, in, x, %, or a bare number, using a dot as the decimal separator.
  2. Replace 'px' with 'pt'; replace ',' decimal with '.'.
  3. Strip surrounding whitespace and stray characters before passing.

Example fix

// before
spacing = "12px"     // 'px' is not a supported unit
spacing = "1,5cm"    // comma decimal separator rejected
// after
spacing = "12pt"
spacing = "1.5cm"
Defensive patterns

Strategy: validation

Validate before calling

static bool IsFiniteNumberWithOptionalUnit(string v)
{
    var t = v.Trim().TrimEnd('x','X','%');
    foreach (var u in new[]{"pt","cm","in"}) if (t.EndsWith(u, System.StringComparison.OrdinalIgnoreCase)) { t = t[..^u.Length]; break; }
    return double.TryParse(t.Trim(), System.Globalization.CultureInfo.InvariantCulture, out var n)
           && !double.IsNaN(n) && !double.IsInfinity(n);
}

Try / catch

try { /* parse spacing */ }
catch (System.ArgumentException ex) when (ex.Message.Contains("finite number"))
{ /* tell user the accepted formats: 12pt / 1.5x / 150% / 0.5cm / 0.5in */ }

Prevention

When it happens

Trigger: Passing a non-numeric value, an unsupported unit (e.g. '12px'), an empty string, a locale-formatted number using comma as decimal ('1,5x'), or a stray suffix/whitespace inside the number portion like '1.5 x' or '12 pt ' where the unit strip leaves '12 ' handled, but 'abc' or '1.2.3' fails TrParse.

Common situations: Typo in the unit suffix ('px' vs 'pt'); locale decimal separator; copy-paste bringing an invisible character; passing a percentage into a field expecting a length.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/cfe22e5b31ebd765. Report an issue: GitHub.