Humanizr/Humanizer · error · ArgumentException

Input words cannot be empty.

Error message

Input words cannot be empty.

What it means

This ArgumentException is thrown inside GreedyCompoundWordsToNumberConverter.TryConvert when the input is null, empty, or whitespace. It throws from the Try-variant, meaning TryToNumber will throw rather than return false for blank input. The greedy tokenizer needs at least one character to attempt matching.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/GreedyCompoundWordsToNumberConverter.cs:38

    {
        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 trimmed = words.Trim();
        var normalized = Normalize(trimmed);
        var negative = false;

        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. Guard with string.IsNullOrWhiteSpace(words) before calling either API.
  2. Handle blank input as a business decision at the call site (skip, default to zero, or prompt the user).
  3. Enable nullable reference types to catch null at compile time.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

// TryToNumber also throws on empty — 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 with null, string.Empty, or whitespace on a locale backed by GreedyCompoundWordsToNumberConverter.

Common situations: User input fields that may be blank; deserialized payloads with omitted fields; strings emptied by normalization or trimming before the call; nullable strings passed without a null check.

Related errors


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