开发者

What is the most appropriate exception to throw when arguments are in conflict with eachother?

开发者 https://www.devze.com 2023-04-05 04:56 出处:网络
I\'m sorry if this has been asked before - but I couldn\'t find a similar question. I have an object which represents a query and an ordering specification for the results. I would like the semantics

I'm sorry if this has been asked before - but I couldn't find a similar question.

I have an object which represents a query and an ordering specification for the results. I would like the semantics to be that you can specify null for the query (i.e. return all results), that you can specify null for the order (i.e. return results in any order), but you cannot specify null for both (because then why are you using the query engine...)

So, ArgumentException, ArgumentNullException and ArgumentOutOfRangeException didn't seem right because it's no开发者_StackOverflow中文版t one argument - and their doc comments all refer to the singular.

Any pointers?

Thanks.


If this is simply to help developers when using your method then go for a standard InvalidOperationException or ArgumentException, with a helpful message, and leave it at that. Put time into more important areas.

If this is going to be driven from the UI or some other process, then you shouldn't ever need to hit the exception as it will be better to validate the input. If you do input validation inside the method, then perhaps consider a custom InvalidInputException, again with a message (I'm not going to start the "are input errors exceptions?" debate...).

Don't worry too much about the specifics of the documentation. So long as the usage follows some measure of common sense, your colleagues will be able to understand what is going on.


I think a NotSupportedException would describe what you are getting at


There are precedents in the .NET Framework for the situation where a check depends on the value of more than one argument.

For example, ArrayList.CopyTo(int index, Array array, int arrayIndex, int count).

In this case, if (index + count) is creater than the size of the ArrayList, an ArgumentException is thrown, using the overload of the ArgumentException constructor that doesn't take a parameterName argument.

This seems similar to your situation.

0

精彩评论

暂无评论...
验证码 换一张
取 消