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

后端 未结 5 2329
佛祖请我去吃肉
佛祖请我去吃肉 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:48

    If you were using Code Contracts (something I HIGHLY recommend), you would put this at the start of the method:

    Contract.Requires(value == SomeEnum.One || value == SomeEnum.Two);
    

    If you want to check a range of an enum which has too many individual values to write them all explicitly, you can do it like this:

    Contract.Requires(SomeEnum.One <= value && value <= SomeEnum.Two);
    

提交回复
热议问题