dotnet/wpf · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

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

What it means

PrintCapabilities.SupportsCapability validates the CapabilityName parameter against PrintSchema.CapabilityNameEnumMin..Max before indexing the internal feature table. Passing a value outside the defined CapabilityName enum range throws ArgumentOutOfRangeException with parameter 'feature'.

Solutions

  1. Pass only defined CapabilityName enum members
  2. Validate raw ints with Enum.IsDefined before casting to CapabilityName
  3. Catch ArgumentOutOfRangeException when enumerating unknown capabilities

Example fix

// before
for (int i = 0; i < count; i++)
    caps.SupportsCapability((CapabilityName)i);
// after
if (Enum.IsDefined(typeof(CapabilityName), i))
    caps.SupportsCapability((CapabilityName)i);
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsValidCapabilityName(CapabilityName f) => f >= PrintSchema.CapabilityNameEnumMin && f <= PrintSchema.CapabilityNameEnumMax;

Type guard

static bool IsDefinedCapability(object v) => v is CapabilityName c && c >= PrintSchema.CapabilityNameEnumMin && c <= PrintSchema.CapabilityNameEnumMax;

Try / catch

try { supports = caps.SupportsCapability(feature); }
catch (ArgumentOutOfRangeException) { supports = false; }

Prevention

When it happens

Trigger: Calling printCapabilities.SupportsCapability((CapabilityName)someInt) where someInt is not a defined CapabilityName member, often from data-driven code that iterates raw ints.

Common situations: Looping capability checks with an upper bound larger than the enum; persisted capability lists from an older framework version containing removed values.

Understand the failure class

Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5b5da3e0b6c94dcc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PrtCap_Public.cs:110

        #endregion Constructors

        #region Public Methods

        /// <summary>
        /// Gets a Boolean value indicating whether the device supports a specific capability.
        /// </summary>
        /// <param name="feature">Capability feature to check for device support.</param>
        /// <returns>True if the capability is supported. False otherwise.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The <paramref name="feature"/> parameter is not a standard feature defined in <see cref="CapabilityName"/>.
        /// </exception>
        public bool SupportsCapability(CapabilityName feature)
        {
            // Verify input parameter
            if (feature < PrintSchema.CapabilityNameEnumMin ||
                feature > PrintSchema.CapabilityNameEnumMax)
            {
                throw new ArgumentOutOfRangeException(nameof(feature));
            }

            return (_pcRootFeatures[(int)feature] != null);
        }

        #endregion Public Methods

        #region Public Properties

        /// <summary>
        /// Gets a <see cref="DocumentCollateCapability"/> object that specifies the device's document collate capability.
        /// </summary>
        /// <remarks>
        /// If the device doesn't support this capability, null reference will be returned.
        /// You should use <see cref="SupportsCapability"/> to check the capability is supported before accessing the returned reference.
        /// </remarks>
        public DocumentCollateCapability DocumentCollateCapability
        {

View on GitHub (pinned to 81131a70a4)