Humanizr/Humanizer · error · ArgumentException

Input words cannot be empty.

Error message

Input words cannot be empty.

What it means

This ArgumentException is thrown inside EastAsianPositionalWordsToNumberConverter.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 converter has no tokens to match against an empty string, so the guard prevents a meaningless parse.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/EastAsianPositionalWordsToNumberConverter.cs:30

    {
        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 = words.Replace(" ", string.Empty).Trim();
        var negative = false;

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

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

        // Exact ordinals are checked before stripping ordinal affixes because some locales encode

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Check string.IsNullOrWhiteSpace(words) before calling either API.
  2. Return a default or skip the call for blank input at the boundary.
  3. Use nullable reference types to surface null propagation at compile time.

Example fix

// before
long value = text.ToNumber(new CultureInfo("zh"));

// after
if (string.IsNullOrWhiteSpace(text))
    return 0;
long value = text.ToNumber(new CultureInfo("zh"));
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(new CultureInfo("zh"));
}
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-only input on a locale backed by EastAsianPositionalWordsToNumberConverter.

Common situations: Unvalidated text from user input or deserialized data; strings that became empty after trimming; optional fields that were not populated in a payload.

Related errors


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