Throwing exceptions in switch statements when no specified case can be handled

后端 未结 9 1575
你的背包
你的背包 2021-01-30 15:52

Let\'s say we have a function that changes a password for a user in a system in an MVC app.:

public JsonResult ChangePassword
    (string username, string curren         


        
相关标签:
9条回答
  • 2021-01-30 16:39

    You do state that you're being very defensive and I might almost say this is overboard. Isn't the other developer going to test their code? Surely, when they do the simplest of tests they'll see that the user can still log in - so, then they'll realize what they need to fix. What you're doing isn't horrible or wrong, but if you're spending a lot of your time doing it, it might just be too much.

    0 讨论(0)
  • 2021-01-30 16:40

    With what you have it is fine although the break statement after it will never be hit because execution of that thread will cease when an exception is thrown and unhandled.

    0 讨论(0)
  • 2021-01-30 16:40

    I've used this practice before for specific instances when the enumeration lives "far" from it's use, but in cases where the enumeration is really close and dedicated to specific feature it seems like a little bit much.

    In all likelihood, I suspect an debug assertion failure is probably more appropriate.

    http://msdn.microsoft.com/en-us/library/6z60kt1f.aspx

    Note the second code sample...

    0 讨论(0)
提交回复
热议问题