Is there a standard .NET exception to throw when a class doesn't implement a required attribute?
        Posted  
        
            by a typing monkey
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by a typing monkey
        
        
        
        Published on 2010-04-13T22:09:54Z
        Indexed on 
            2010/04/13
            22:13 UTC
        
        
        Read the original article
        Hit count: 339
        
Suppose I want to throw a new exception when invoking a generic method with a type that doesn't have a required attribute. Is there a .NET exception that's appropriate for this situation, or, more likely, one that would be a suitable ancestor for a custom exception?
For example:
public static class ClassA
{
    public static T DoSomething<T>(string p)
    {
        Type returnType = typeof(T);
        object[] typeAttributes = returnType.GetCustomAttributes(typeof(SerializableAttribute), true);
        if ((typeAttributes == null) || (typeAttributes.Length == 0))
        {
             // Is there an exception type in the framework that I should use/inherit from here?
             throw new Exception("This class doesn't support blah blah blah"); 
             // Maybe ArgumentException? That doesn't seem to fit right.
        }
    }
} 
Thanks.
© Stack Overflow or respective owner