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

前端 未结 12 2299
青春惊慌失措
青春惊慌失措 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:53

    I would suggest to use either NotImplementedException or better a custom DrivingStateNotImplementedException if you like to throw exceptions.

    Me, I would use a default drivingstate for default (like neutral/stop) and log the missing driverstate (because it's you that missed the drivingstate, not the customer)

    It's like a real car, cpu decides it misses to turn on the lights, what does it do, throw an exception and "break" all control, or falls back to a known state which is safe and gives a warning to the driver "oi, I don't have lights"

提交回复
热议问题