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


I've been writing C # for the past 8 years and have become a very protective OOP programmer. Working in a statically typed language, you validate the arguments in such methods and leave the exception, where you will not be in dynamic and more liberal languages. I consider myself a specialist in C # and others are looking for feedback from best C # developers on best practices.

Let's say we have an MVC app which is a function.:

  public JsonResult ChangePassword (string username, string currentPassword, string newPassword) {switch (this. membershipService.ValidateLogin (username, currentPassword)) {case UserValidationResult.BasUsername: case UserValidationResult.BadPassword: // abort: invalid JsonResult return // wrong password username / pass case combo UserValidationResult.TrialExpired // abortion: local Returns JsonResult // with error message that is user Can not log in because he UserValidationResult.Success: Break case ended its trial period; } // Now password is now that the user is validated}}  

membershipService.ValidateLogin () returns a UserValidationResult as the form of enum have been defined:

  enum UserValidationResult {BadUsername, BadPassword, TrialExpired, success}  

a defensive programmer, I ChangePassword () up Being changed will be method, then return an unrecognized exception to UserValidationResult ValidateLogin () return value:

  public JsonResult ChangePassword (string aligned User name, string currentPassword, string newPassword) {switch (this.membershipService.ValidateLogin (username, currentPassword)) {case UserValidationResult.BasUsername: case UserValidationResult.BadPassword: // abort: JsonResult return // invalid user with local error message Name / Pass Combo Case UserValidationResult.TrialExpired / abort: with JsonResult local error message // This user can not log in because their trial period has expired UserValidationResult.Success: break; Default: New NotImplementedException Throw ("Unrecognized UserValidationResult Value."); // or NotSupportedException () break; } // Now change password that the user is valid  

I always considered a pattern like the previous snippet on an ideal exercise. For example, what would happen if a developer is required, now when users try to login, for this business reason, do they first consider contacting the business? So UserValidationResult has been updated to the definition:

  enum UserValidationResult {BadUsername, BadPassword, test, compile, ContactUs, success}  

developer new enum values ​​( UserValidationResult.ContactUs ) back is to ValidateLogin () to change the body of law, but Forgot to update ChangePassword () . Without exception in the switch, the user is allowed to change his password even when his login attempt should not be valid even at the first place!

Is it just me, or is it the default of : throw a new exception () is a good idea? I saw it a few years ago and always (after provoking it) it is considered a best practice.

I always throw exceptions in this case. Consider using, which gives rich information in this situation.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -