Is there a standard .NET throw exception when a class does not implement the required attribute?

Suppose I want to create a new exception when calling a generic method with a type that does not have a required attribute. Is there a .NET exception that is suitable for this situation or, more likely, a suitable ancestor for a custom exception?

For instance:

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.

+4
source share
4 answers

as I see it, you can go in one of three ways ...
1) NotSupportedException
2) NotImplementedException
3) You can create your own exception type

+10
source

I know that some of the built-in code templates throw a NotImplemented exception as a placeholder, so this may be a good place to start.

+3
source

NotSupportedException is a good choice, I think. Also TargetException and TargetInvocationException are reflection specific.

0
source

NotSupportedException as NotImplemented technically for stubs, which is not what happens here. Type is not supported.

Saying, I do not know why MS did not provide a canonical list with their internal definitions. It strikes the mind.

0
source

Source: https://habr.com/ru/post/1306859/


All Articles