I’m using custom attributes to attach meta-data to class methods. During run-time, those attributes, and their parameters, are validated. Is there a consensus about what existing exception class to throw should an attribute (or attribute parameter) prove invalid?
For instance, given an attribute taking a instance method name as its constructor parameter:
public class StateAttribute : Attribute {
public string ParentState { get; set; }
public StateAttribute() {}
}
when used in a class like:
public StateMachine {
[State]
public Result TopState(Event e) { ... }
[State( ParentState = "TopState" )]
public Result NestedState(Event e) { ... }
}
An initialization routine will walk all the methods to which the State attribute has been applied, and resolve the ParentState name to the actual MethodInfo. If (eg. due to a typo) no method with that name can be found, it needs to throw an exception. Since I don’t want to invent new exception classes, which one would you suggest?
And just to be sure, there’s no way to validate the method names during compile time, right?
InvalidOperationExceptionis the all-purpose one I personally use when “something has gone wrong”.But to be honest, this sounds like a perfect case for deriving your own Exception to me.