Correct exception to throw for an unhandled switch case for an argument?

后端 未结 5 2257
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 22:26

NOTE: This is different than the proposed duplicates as this deals with an argument rather than a value. The behavior and applicable scenarios are essentially different.

5条回答
  •  無奈伤痛
    2021-02-19 22:35

    ArgumentException looks the most correct to me in this instance (though is not defined in the BCL).

    There is a specialized exception for enum arguments - InvalidEnumArgumentException:

    The exception thrown when using invalid arguments that are enumerators.

    An alternative is ArgumentOutOfRangeException:

    The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method.

    The logic for using these is that the passed in argument (value) is not valid as far as someFunc is concerned.

提交回复
热议问题