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 encodeView on GitHub (pinned to ffc2b77c0f)
Solutions
- Check string.IsNullOrWhiteSpace(words) before calling either API.
- Return a default or skip the call for blank input at the boundary.
- 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
- Always guard with string.IsNullOrWhiteSpace before calling ToNumber or TryToNumber.
- Handle blank input as a business decision at the call site.
- Enable nullable reference types to catch null propagation at compile time.
- Validate at the trust boundary before data reaches the parser.
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
- Input words cannot be empty.
- Input words cannot be empty.
- Input words cannot be empty.
- Input words cannot be empty.
- Unrecognized number word: {unrecognizedWord}
AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13).
Data as JSON: /api/errors/7843b76d5c88215a.
Report an issue: GitHub.