Humanizr/Humanizer · error · ArgumentException

Input words cannot be empty.

Error message

Input words cannot be empty.

What it means

This ArgumentException is thrown inside CompoundScaleWordsToNumberConverter.TryConvert when the input string is null, empty, or whitespace. Critically, it throws from the Try-variant — not just the throwing Convert — so callers who expect TryConvert/TryToNumber to return false on invalid input will instead receive an exception. The guard exists because an empty phrase has no parseable tokens and the downstream tokenizer would produce meaningless diagnostics.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/CompoundScaleWordsToNumberConverter.cs:41

    {
        if (!TryConvert(words, out var parsedValue, out var unrecognizedWord))
        {
            throw new ArgumentException($"Unrecognized number word: {unrecognizedWord}");
        }

        return parsedValue;
    }

    /// <inheritdoc />
    public override bool TryConvert(string words, out long parsedValue) =>
        TryConvert(words, out parsedValue, out _);

    /// <inheritdoc />
    public override bool TryConvert(string words, out long parsedValue, out string? unrecognizedWord)
    {
        if (string.IsNullOrWhiteSpace(words))
        {
            throw new ArgumentException("Input words cannot be empty.");
        }

        var normalized = Normalize(words);
        var negative = false;

        // Negative handling is deliberately outside the main parser so the cardinal and ordinal
        // rules can stay focused on the positive phrase grammar.
        foreach (var negativePrefix in profile.NegativePrefixes)
        {
            if (!normalized.StartsWith(negativePrefix, StringComparison.Ordinal))
            {
                continue;
            }

            negative = true;
            normalized = normalized[negativePrefix.Length..].Trim();
            break;
        }

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Check string.IsNullOrWhiteSpace(words) before calling ToNumber or TryToNumber and handle the empty case explicitly.
  2. Use a null-coalescing or guard clause at the entry point: if (string.IsNullOrWhiteSpace(input)) return;
  3. Enable nullable reference types so the compiler warns when a possibly-null string reaches the call.
  4. Treat empty input as a business-logic decision (zero, skip, or error) rather than letting it reach the parser.

Example fix

// before
long value = userInput.ToNumber(culture);

// after
if (string.IsNullOrWhiteSpace(userInput))
    return 0;
long value = userInput.ToNumber(culture);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(words))
    return 0;
return words.ToNumber(culture);

Type guard

static bool IsNonEmpty(string? words) =>
    !string.IsNullOrWhiteSpace(words);

Try / catch

// TryToNumber ALSO throws on empty input, so guard before calling
try
{
    return words.ToNumber(culture);
}
catch (ArgumentException ex) when (ex.Message.Contains("cannot be empty"))
{
    return 0;
}

Prevention

When it happens

Trigger: Calling ToNumber or TryToNumber on a string that is null, string.Empty, or contains only whitespace, for any locale backed by CompoundScaleWordsToNumberConverter.

Common situations: Passing user input from a text field without null/empty checks; chaining a ToNumber call on the result of a Trim that produced an empty string; deserialized or API-received payload where the field was omitted; nullable reference types not enforced at the boundary.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/9393b9d0accd58de. Report an issue: GitHub.