加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 返回异常值

发布时间:2020-12-16 07:33:35 所属栏目:asp.Net 来源:网络整理
导读:为什么这段代码不能编译? 它给了我错误: not all code paths return a value 码: public bool isUserProfileHashed(string username){ bool isHashed = false; MembershipUser u = null; u = Membership.GetUser(username); if (u != null) { try { u.Get
为什么这段代码不能编译?
它给了我错误:

not all code paths return a value

码:

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之外放一个返回,把它放在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;
}

[编辑]删除不必要的回报(@FredrikM?rk)未使用的捕获异常因此也将其删除.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读