Programming against an enum in a switch statement, is this your way to do?

前端 未结 12 2213
青春惊慌失措
青春惊慌失措 2020-12-24 09:15

Look at the code snippet:

This is what I normally do when coding against an enum. I have a default escape with an InvalidOperationException (I do not use ArgumentExc

12条回答
  •  囚心锁ツ
    2020-12-24 09:47

    What you should do if you encounter an unhandled enum value of course depends on the situation. Sometimes it's perfectly legal to only handle some of the values.

    If it's an error that you have an unhandles value you should definitely throw an exception just like you do in the example (or handle the error in some other way). One should never swallow an error condition without producing an indication that there is something wrong.

    A default case with just a break doesn't smell very good. I would remove that to indicate the switch doesn't handle all values, and perhaps add a comment explaining why.

提交回复
热议问题