3

When the value of a normal parameter is unacceptable (and the requirements can't be checked completely at compile time), we check at runtime and throw an ArgumentException if the check fails.

When the "value" of a type parameter is similarly unacceptable, what kind of exception do we throw? Are there any guidelines on this?

(Note: If I try ArgumentException, ReSharper wants to complain - which makes me think there may be another kind of exception that's recommended for this.)

  • What is the complaint from Resharper? – Robert Harvey Oct 16 '14 at 20:52
  • @RobertHarvey "Cannot resolve symbol T", where T is the type parameter. – Theodoros Chatzigiannakis Oct 16 '14 at 20:53
  • Well, ArgumentException generally refers to the formal parameter list, and the name of the offending parameter is the usual argument to ArgumentException's constructor, so Resharper is basically complaining that you're referring to a parameter name that's not one of the declared method parameters (when you throw the exception). Can you use a Type Constraint instead? – Robert Harvey Oct 16 '14 at 20:55
  • @RobertHarvey Yes, I understand that. In my method, any object is acceptable as an argument (so no problem there), but only specific types are acceptable for the type parameter T (specifically, public interfaces - neither of which can be expressed in the constraint). So I'd like to know if there is perhaps another kind of exception that I should be throwing in this case. (Resharper's complaint isn't my main concern - maybe I should remove that note.) – Theodoros Chatzigiannakis Oct 16 '14 at 20:58
  • 2
    See stackoverflow.com/q/1412598 – Robert Harvey Oct 16 '14 at 20:58
3

There isn't a native exception in the .NET Framework for "Invalid Type Parameter Argument." Jon Skeet asked about this on Stack Overflow, and ultimately decided to write his own:

#region License and Terms
// Unconstrained Melody
// Copyright (c) 2009-2011 Jonathan Skeet. All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Runtime.Serialization;

namespace UnconstrainedMelody
{
    /// <summary>
    /// Exception thrown to indicate that an inappropriate type argument was used for
    /// a type parameter to a generic type or method.
    /// </summary>
    public class TypeArgumentException : Exception
    {
        /// <summary>
        /// Constructs a new instance of TypeArgumentException with no message.
        /// </summary>
        public TypeArgumentException()
        {
        }

        /// <summary>
        /// Constructs a new instance of TypeArgumentException with the given message.
        /// </summary>
        /// <param name="message">Message for the exception.</param>
        public TypeArgumentException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Constructs a new instance of TypeArgumentException with the given message and inner exception.
        /// </summary>
        /// <param name="message">Message for the exception.</param>
        /// <param name="inner">Inner exception.</param>
        public TypeArgumentException(string message, Exception inner)
            : base(message, inner)
        {
        }

        /// <summary>
        /// Constructor provided for serialization purposes.
        /// </summary>
        /// <param name="info">Serialization information</param>
        /// <param name="context">Context</param>
        protected TypeArgumentException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.