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.:
membershipService.ValidateLogin()
返回一个 UserValidationResult
枚举,定义为:
membershipService.ValidateLogin()
returns a UserValidationResult
enum that is defined as:
作为一名防御性程序员,如果从 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()
:
我一直认为像上面最后一个片段这样的模式是最佳实践.例如,如果一个开发人员要求现在当用户尝试登录时,如果出于这个或那个业务原因,他们应该首先联系业务怎么办?所以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:
开发人员更改 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 语句中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!