Humanizr/Humanizer · error · ArgumentException

Input words cannot be empty.

Error message

Input words cannot be empty.

What it means

This ArgumentException is thrown inside ContractedScaleWordsToNumberConverter.TryConvert when the input is null, empty, or whitespace. Notably, it throws from the Try-variant (TryConvert / TryToNumber), not only from the throwing Convert method. Callers who expect TryToNumber to return false for blank input will instead receive an exception because the parser has no tokens to process.

Source

Thrown at src/Humanizer/Localisation/WordsToNumber/ContractedScaleWordsToNumberConverter.cs:42

    {
        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 parsing is kept outside the token state machine so the main loop can stay
        // focused on contracted scale composition.
        if (normalized.StartsWith(profile.MinusWord + " ", StringComparison.Ordinal))
        {
            negative = true;
            normalized = normalized[(profile.MinusWord.Length + 1)..].Trim();
        }

        if (TryParseCardinal(normalized, out var value))
        {
            parsedValue = value;
            if (negative)
            {

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Guard with string.IsNullOrWhiteSpace(words) before calling either ToNumber or TryToNumber.
  2. Return early or use a sentinel value (0, null) for empty input at the call site.
  3. Enable nullable reference types to catch null propagation at compile time.

Example fix

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

// after
long value = string.IsNullOrWhiteSpace(raw) ? 0 : raw.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 a whitespace-only string on a locale backed by ContractedScaleWordsToNumberConverter.

Common situations: Unvalidated user text from an input field; trimmed strings that became empty; deserialized payloads with missing fields; nullable strings passed without a null check.

Related errors


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