当无法处理指定大小写时,在 switch 语句中抛出异常 [英] Throwing exceptions in switch statements when no specified case can be handled

查看:31
本文介绍了当无法处理指定大小写时,在 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 语句中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆