LykosAI/StabilityMatrix · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values.

Error message

Specified argument was out of range of valid values.

What it means

NumberFormatModeSampleConverter.Convert formats a sample number according to a NumberFormatMode enum. The switch handles Default, CurrentCulture, and InvariantCulture; any other value (uninitialized, out-of-range enum, or an enum member added later without updating this switch) falls through to the default arm and throws ArgumentOutOfRangeException with no parameter name or value.

Solutions

  1. Pass a valid NumberFormatMode value (Default, CurrentCulture, or InvariantCulture) as the converter parameter.
  2. If you added a new enum member, add a corresponding case to the switch in Convert.
  3. Validate/cast the incoming parameter with Enum.IsDefined before formatting.

Example fix

// before
_ => throw new ArgumentOutOfRangeException()

// after
var mode = Enum.IsDefined(typeof(NumberFormatMode), rawMode)
    ? rawMode : NumberFormatMode.Default;
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(NumberFormatMode), mode))
    mode = NumberFormatMode.Default;

Type guard

static bool IsValidMode(NumberFormatMode mode) => mode is NumberFormatMode.Default or NumberFormatMode.CurrentCulture or NumberFormatMode.InvariantCulture;

Try / catch

try
{
    return converter.Convert(sample, typeof(string), modeParam, culture)?.ToString();
}
catch (ArgumentOutOfRangeException)
{
    return sample.ToString("N2", CultureInfo.CurrentCulture);
}

Prevention

When it happens

Trigger: Calling Convert with a converter parameter (or bound enum value) that is not one of the three defined NumberFormatMode members, passing an uninitialized (0) enum value that was not mapped to a case, or adding a new NumberFormatMode member without extending the switch.

Common situations: Enum evolution after adding a new format mode; passing an invalid int cast to NumberFormatMode as the converter parameter; deserializing a settings value into the enum that is outside the defined range.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/35819b2b7c90e185. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Converters/NumberFormatModeSampleConverter.cs:27

/// Converts a <see cref="NumberFormatMode"/> to a sample number string
/// </summary>
public class NumberFormatModeSampleConverter : IValueConverter
{
    /// <inheritdoc />
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is not NumberFormatMode mode)
            return null;

        const double sample = 12345.67;

        // Format the sample number based on the number format mode
        return mode switch
        {
            NumberFormatMode.Default => sample.ToString("N2", culture),
            NumberFormatMode.CurrentCulture => sample.ToString("N2", culture),
            NumberFormatMode.InvariantCulture => sample.ToString("N2", CultureInfo.InvariantCulture),
            _ => throw new ArgumentOutOfRangeException()
        };
    }

    /// <inheritdoc />
    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

View on GitHub (pinned to af93d6ef57)