stride3d/stride · error · ArgumentOutOfRangeException

There must be four and only four input values for Half4.

Error message

There must be four and only four input values for Half4.

What it means

Thrown by the Half4(Half[]) constructor when the input array does not contain exactly four elements. Half4 is a fixed-size 4-component vector (X, Y, Z, W), so it can only be built from exactly four Half values. Thrown as ArgumentOutOfRangeException for the 'values' parameter.

Solutions

  1. Ensure the array has exactly 4 elements before constructing
  2. Verify binary read/stride math so each attribute yields exactly 4 halves
  3. Handle truncated data explicitly at load time instead of passing short arrays to the constructor

Example fix

// before
var c = new Half4(record); // record.Length == 3 -> throws
// after
if (record.Length != 4) throw new InvalidDataException($"Expected 4 halves, got {record.Length}");
var c = new Half4(record);
Defensive patterns

Strategy: validation

Validate before calling

if (values is null) throw new ArgumentNullException(nameof(values));
if (values.Length != 4) throw new ArgumentException($"Half4 requires exactly 4 values, got {values.Length}.", nameof(values));

Type guard

static bool IsValidHalf4Source(Half[]? values) => values is { Length: 4 };

Try / catch

try { var v = new Half4(values); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "values")
{
    // handle truncated record
    var v = new Half4(Half.Zero, Half.Zero, Half.Zero, Half.Zero);
}

Prevention

When it happens

Trigger: Calling new Half4(Half[]) with an array of length 0–3 or 5+, e.g. a truncated packed color/quantized attribute record or an over-long interleaved buffer slice.

Common situations: Reading half-precision vertex attributes from binary mesh files with wrong stride; serialized color data missing an alpha channel; end-of-file truncation producing short arrays.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/db0f495e0de812ec. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Mathematics/Half4.cs:134

    public Half4(Half value)
    {
        X = value;
        Y = value;
        Z = value;
        W = value;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Half4"/> struct.
    /// </summary>
    /// <param name="values">The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
    public Half4(Half[] values)
    {
        ArgumentNullException.ThrowIfNull(values);
        if (values.Length != 4)
            throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Half4.");

        X = values[0];
        Y = values[1];
        Z = values[2];
        W = values[3];
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Half4"/> structure.
    /// </summary>
    /// <param name="x">The X component.</param>
    /// <param name="y">The Y component.</param>
    /// <param name="z">The Z component.</param>
    /// <param name="w">The W component.</param>
    public Half4(float x, float y, float z, float w)
    {
        X = (Half)x;
        Y = (Half)y;

View on GitHub (pinned to 96fad776d2)