gpt4 book ai didi

asp.net - 异常时返回值

转载 作者:行者123 更新时间:2023-12-02 13:53:49 27 4
gpt4 key购买 nike

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

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;
}

最佳答案

您忘记将 return 放在 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;
}

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com