LykosAI/StabilityMatrix · error · InvalidEnumArgumentException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'NanHandling')

What it means

In the same Unbox NaN path, the switch's default arm assumes any NanHandling other than DefaultValue/Throw is an InvalidEnumArgumentException with parameter 'NanHandling'. Hitting it with message "Specified argument was out of range of valid values. (Parameter 'NanHandling')" means NanHandling held an undefined enum value (out-of-range cast or corrupt state).

Solutions

  1. Set NanHandling to a valid ReturnBehavior member (DefaultValue or Throw)
  2. Validate enum values in XAML (use x:Static instead of raw ints)
  3. Add an Enum.IsDefined check when constructing the converter from untrusted input
  4. Update persisted settings that reference a renamed/removed enum member

Example fix

// before
<conv:NullableDefaultNumericConverter NanHandling="3"/>
// after
<conv:NullableDefaultNumericConverter NanHandling="DefaultValue"/>
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(ReturnBehavior), nanHandling))
    nanHandling = ReturnBehavior.DefaultValue;

Type guard

bool IsValidReturnBehavior(ReturnBehavior b) => b is ReturnBehavior.DefaultValue or ReturnBehavior.Throw;

Try / catch

try { result = converter.ConvertBack(v, t, p, c); }
catch (InvalidEnumArgumentException ex) { Log.Error(ex, "Invalid NanHandling"); result = 0d; }

Prevention

When it happens

Trigger: NanHandling set to an int not defined in ReturnBehavior (e.g. via XAML attribute parsing, reflection, or a bad cast), causing the switch to fall into the default arm.

Common situations: Binding a numeric XAML attribute to ReturnBehavior with an invalid value; a versioned enum change making stored settings reference a removed member; constructing the converter with (ReturnBehavior)99.

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/b8579b44f78594d9. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs:36

    /// <summary>
    /// Unboxes a nullable value type
    /// </summary>
    private TSource Unbox(TTarget? value)
    {
        if (!value.HasValue)
        {
            return default;
        }

        if (TTarget.IsNaN(value.Value))
        {
            return NanHandling switch
            {
                ReturnBehavior.DefaultValue => default,
                ReturnBehavior.Throw
                    => throw new InvalidCastException("Cannot convert NaN to a numeric type"),
                _
                    => throw new InvalidEnumArgumentException(
                        nameof(NanHandling),
                        (int)NanHandling,
                        typeof(ReturnBehavior)
                    )
            };
        }

        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));
    }

    /// <summary>
    /// Convert a value type to a nullable value type
    /// </summary>
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))
        {
            // ReSharper disable once LocalizableElement

View on GitHub (pinned to af93d6ef57)