Throwing exceptions in switch statements when no specified case can be handled(当无法处理指定大小写时,在 switch 语句中抛出异常)
问题描述
假设我们有一个函数可以在 MVC 应用程序中更改系统中用户的密码.:
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 currentPassword, string newPassword)
{
switch (this.membershipService.ValidateLogin(username, currentPassword))
{
case UserValidationResult.BasUsername:
case UserValidationResult.BadPassword:
// abort: return JsonResult with localized error message
// for invalid username/pass combo.
case UserValidationResult.TrialExpired
// abort: return JsonResult with localized error message
// that user cannot login because their trial period has expired
case UserValidationResult.Success:
break;
}
// NOW change password now that user is validated
}
membershipService.ValidateLogin() 返回一个 UserValidationResult 枚举,定义为:
membershipService.ValidateLogin() returns a UserValidationResult enum that is defined as:
enum UserValidationResult
{
BadUsername,
BadPassword,
TrialExpired,
Success
}
作为一名防御性程序员,如果从 ValidateLogin()<返回无法识别的 UserValidationResult 值,我将更改上述 ChangePassword() 方法以引发异常/代码>:
Being a defensive programmer, I would change the above ChangePassword() method to throw an exception if there's an unrecognized UserValidationResult value back from ValidateLogin():
public JsonResult ChangePassword
(string username, string currentPassword, string newPassword)
{
switch (this.membershipService.ValidateLogin(username, currentPassword))
{
case UserValidationResult.BasUsername:
case UserValidationResult.BadPassword:
// abort: return JsonResult with localized error message
// for invalid username/pass combo.
case UserValidationResult.TrialExpired
// abort: return JsonResult with localized error message
// that user cannot login because their trial period has expired
case UserValidationResult.Success:
break;
default:
throw new NotImplementedException
("Unrecognized UserValidationResult value.");
// or NotSupportedException()
break;
}
// Change password now that user is validated
}
我一直认为像上面最后一个片段这样的模式是最佳实践.例如,如果一个开发人员要求现在当用户尝试登录时,如果出于这个或那个业务原因,他们应该首先联系业务怎么办?所以UserValidationResult的定义更新为:
I always considered a pattern like the last snippet above a best practice. For example, what if one developer gets a requirement that now when users try to login, if for this-or-that business reason, they're suppose to contact the business first? So UserValidationResult's definition is updated to be:
enum UserValidationResult
{
BadUsername,
BadPassword,
TrialExpired,
ContactUs,
Success
}
开发人员更改 ValidateLogin() 方法的主体以在适用时返回新的枚举值 (UserValidationResult.ContactUs),但忘记更新 ChangePassword()代码>.如果没有开关中的异常,当用户的登录尝试一开始不应该被验证时,仍然允许用户更改密码!
The developer changes the body of the ValidateLogin() method to return the new enum value (UserValidationResult.ContactUs) when applicable, but forgets to update ChangePassword(). Without the exception in the switch, the user is still allowed to change their password when their login attempt shouldn't even be validated in the first place!
只是我,还是这个 default: throw new Exception() 是个好主意?几年前我看到了它,并且总是(在摸索之后)认为它是最佳实践.
Is it just me, or is this default: throw new Exception() a good idea? I saw it some years ago and always (after groking it) assume it to be a best practice.
推荐答案
在这种情况下我总是抛出异常.考虑使用 InvalidEnumArgumentException,在这种情况下提供更丰富的信息.
I always throw an exception in this case. Consider using InvalidEnumArgumentException, which gives richer information in this situation.
这篇关于当无法处理指定大小写时,在 switch 语句中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当无法处理指定大小写时,在 switch 语句中抛出异常
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
