4 asp.net exception-handling exception asp.net-2.0
为什么这段代码不能编译?
它给了我错误:
并非所有代码路径都返回一个值
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;
}
Run Code Online (Sandbox Code Playgroud)
Bin*_*ony 10
你忘了在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;
}
Run Code Online (Sandbox Code Playgroud)
[编辑]
删除不必要的返回(@FredrikMörk)
未使用的捕获异常因此也将其删除.