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 语句中抛出异常
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01