扔在switch语句中的异常时,没有指定的情况下,可以处理 [英] Throwing exceptions in switch statements when no specified case can be handled
问题描述
我已经写C#在过去的8年,已经变得相当的防守OOP编程。在静态类型语言工作,你不喜欢的东西验证参数的方法,并抛出异常,你就不会在是动态的,更自由的语言。我认为自己在C#中的专家,并正在寻找从最佳做法的其他精通C#开发人员的反馈意见。
I've been writing C# for the last 8 years and have become quite the defensive OOP programmer. Working in a statically-typed language, you do things like validate arguments in methods and throw exceptions where you wouldn't in languages that are dynamic and more liberal. I consider myself an expert in C# and am looking for feedback from other well-versed C# developers on best practices.
让我们说我们有,对于更改密码功能用户在一个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
枚举:
enum UserValidationResult
{
BadUsername,
BadPassword,
TrialExpired,
Success
}
作为一个防御性的程序员,我会改变上述的ChangePassword()
方法抛出一个异常,如果有从 UserValidationResult
值回> ValidateLogin():
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!
难道只是我,或者是这个默认:抛出新的异常()
一个好主意?我看到它在几年前,始终认为这是一个最佳实践(groking后)。
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语句中的异常时,没有指定的情况下,可以处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!