Humanizr/Humanizer · error · ArgumentException

Input words cannot be empty.

Error message

Input words cannot be empty.

What it means

This ArgumentException is thrown inside InvertedTensWordsToNumberConverter.TryConvert when the input is null, empty, or whitespace. It throws from the Try-variant, so TryToNumber will throw rather than return false on blank input. The inverted-tens parser needs at least one token to attempt structural decomposition.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/InvertedTensWordsToNumberConverter.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 normalized = Normalize(words);
        var negative = false;

        foreach (var negativePrefix in profile.NegativePrefixes)
        {
            if (!normalized.StartsWith(negativePrefix, StringComparison.Ordinal))
            {
                continue;
            }

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

        if (profile.AllowInvariantIntegerInput &&

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Check string.IsNullOrWhiteSpace(words) before calling either API.
  2. Return a default or handle the empty case at the call site.
  3. Enable nullable reference types for compile-time null safety.

Example fix

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

// after
if (string.IsNullOrWhiteSpace(input))
    return 0;
long value = input.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 — 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 InvertedTensWordsToNumberConverter.

Common situations: Blank user input; deserialized payloads with missing fields; strings that became empty after trimming; nullable reference not checked before the call.

Related errors


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