为什么这段代码不能编译?
它给了我错误:

代码:

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        try
        {
            u.GetPassword();
        }
        catch (Exception exception)
        {
            // An exception is thrown when the GetPassword method is called for a user with a hashed password
            isHashed = true;
            return isHashed;
        }
        return isHashed;
    }

最佳答案

你忘了在 if 的 之外放一个 return ,把它放在 if 结束大括号之后

public bool isUserProfileHashed(string username)
{
    bool isHashed = false;
    MembershipUser u = null;
    u = Membership.GetUser(username);
    if (u != null)
    {
        try
        {
            u.GetPassword();
        }
        catch
        {
            // An exception is thrown when the GetPassword method is called for a user with a hashed password
            isHashed = true;
        }
    }
    return isHashed;
}

[编辑]
删除不必要的返回(@Fredrik Mörk)
未使用捕获的异常,因此也将其删除。

关于asp.net - 异常时返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1079389/

10-09 21:29