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 'value')

What it means

PageMediaTypeSetting.Value accepts only values within PrintSchema.PageMediaTypeEnumMin..Max; assigning an out-of-range integer (cast to the PageMediaType enum) throws a bare ArgumentOutOfRangeException for 'value'. The setter stores the value as the OptionName property of the print config element.

Solutions

  1. Only assign valid PageMediaType enum members (use Enum.IsDefined before casting).
  2. Clamp or map unknown values to a valid default (e.g. PageMediaType.None).
  3. Validate ints read from config/DB against PrintSchema.PageMediaTypeEnumMin/Max before assignment.
  4. Catch ArgumentOutOfRangeException and fall back to the queue default media type.

Example fix

// before
printTicket.PageMediaType = (PageMediaType)rawInt; // may be undefined
// after
if (Enum.IsDefined(typeof(PageMediaType), rawInt))
    printTicket.PageMediaType = (PageMediaType)rawInt;
Defensive patterns

Strategy: validation

Validate before calling

if (Enum.IsDefined(typeof(PageMediaType), rawValue))
    printTicket.PageMediaType = (PageMediaType)rawValue;

Type guard

bool IsValidPageMediaType(object v) => v is int i && i >= PrintSchema.PageMediaTypeEnumMin && i <= PrintSchema.PageMediaTypeEnumMax;

Try / catch

try { printTicket.PageMediaType = value; }
catch (ArgumentOutOfRangeException) { printTicket.PageMediaType = PageMediaType.None; }

Prevention

When it happens

Trigger: Setting PageMediaType on a PrintTicket to a value outside the defined Print Schema enum range (e.g. an undefined enum member or an invalid int cast).

Common situations: Casting raw ints from config files or device settings into PageMediaType; enum values from older/newer framework versions not present in the schema range; parsing user input without validating the enum.

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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2ab08b1c159a796a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PageMediaType.cs:255

        /// </summary>
        /// <remarks>
        /// If the setting is not specified yet, getter will return 0.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The value to set is not one of the standard <see cref="PageMediaType"/>.
        /// </exception>
        public PageMediaType Value
        {
            get
            {
                return (PageMediaType)this[PrintSchemaTags.Framework.OptionNameProperty];
            }
            set
            {
                if (value < PrintSchema.PageMediaTypeEnumMin ||
                    value > PrintSchema.PageMediaTypeEnumMax)
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                this[PrintSchemaTags.Framework.OptionNameProperty] = (int)value;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Converts the page media type setting to human-readable string.
        /// </summary>
        /// <returns>A string that represents this page media type setting.</returns>
        public override string ToString()
        {
            return Value.ToString();
        }

View on GitHub (pinned to 81131a70a4)