dotnet/wpf · error · ArgumentException

ArgumentException(message, name)

Error message

ArgumentException(message, name)

What it means

Verify.IsTrue(bool expression, string name, string message) throws ArgumentException(message, name) when the supplied expression is false. It is a generic precondition helper: the caller supplies both the parameter name and the explanatory message, so the exception text describes exactly which condition was violated. Unlike Debug.Assert it throws in release builds.

Solutions

  1. Read the exception's Message and ParamName to learn which condition failed; correct that condition at the call site.
  2. Check the API's documentation/usage for the required parameter combination or preconditions.
  3. If the condition depends on initialization order, ensure the object is fully initialized before invoking the API.
  4. Wrap in try/catch only when the failure is an expected, recoverable misuse in your own orchestration code.

Example fix

// before
collection.VerifyIndex(i); // Verify.IsTrue(i >= 0, "index", "index must be >= 0") fails for i = -1
// after
if (i < 0) throw new ArgumentOutOfRangeException(nameof(i));
collection.VerifyIndex(i);
Defensive patterns

Strategy: validation

Validate before calling

if (!precondition)
    throw new ArgumentException($"{nameof(arg)} does not satisfy the required precondition.", nameof(arg));

Type guard

bool MeetsPrecondition(T arg) => /* mirror of the API's required condition */ arg is not null && arg.IsValid;

Try / catch

try
{
    LibraryCall(arg);
}
catch (ArgumentException ex)
{
    logger.LogError("Precondition failed for {Param}: {Message}", ex.ParamName, ex.Message);
    throw;
}

Prevention

When it happens

Trigger: Any call path in WindowsBase that calls Verify.IsTrue and whose precondition fails - e.g. a flag/parameter combination the API forbids, or an object in the wrong state at the call site.

Common situations: Passing mutually exclusive options; calling an API before initialization so an internal boolean precondition is false; misuse of an internal/helper API not intended for public callers.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/Verify.cs:78

        public static void IsNotNull<T>(T obj, string name) where T : class
        {
            if (obj == null)
            {
                throw new ArgumentNullException(name);
            }
        }

        /// <summary>
        /// Verifies the specified expression is true.  Throws an ArgumentException if it's not.
        /// </summary>
        /// <param name="expression">The expression to be verified as true.</param>
        /// <param name="name">Name of the parameter to include in the ArgumentException.</param>
        /// <param name="message">The message to include in the ArgumentException.</param>
        public static void IsTrue(bool expression, string name, string message)
        {
            if (!expression)
            {
                throw new ArgumentException(message, name);
            }
        }

        /// <summary>
        /// Verifies two values are not equal to each other.  Throws an ArgumentException if they are.
        /// </summary>
        /// <param name="actual">The actual value.</param>
        /// <param name="notExpected">The value that 'actual' should not be.</param>
        /// <param name="parameterName">The name to display for 'actual' in the exception if this test fails.</param>
        /// <param name="message">The message to include in the ArgumentException.</param>
        public static void AreNotEqual<T>(T actual, T notExpected, string parameterName, string message)
        {
            if (notExpected == null)
            {
                // Two nulls are considered equal, regardless of type semantics.
                if (actual == null || actual.Equals(notExpected))
                {
                    throw new ArgumentException(SR.Format(SR.Verify_AreNotEqual, notExpected), parameterName);

View on GitHub (pinned to 81131a70a4)