public void GrantAccesOnPage(int AccessPoint1, int AccessPoint2)
{
GlobalVariables.GrantDeny = 0;
string[] arr = {
GlobalVariables.SessionPort1,
GlobalVariables.SessionPort2,
GlobalVariables.SessionPort3,
GlobalVariables.SessionPort4
};
foreach (var r in arr)
{
if (Convert.ToInt32(r) == AccessPoint1 || Convert.ToInt32(r) == AccessPoint2)
{
if (!string.IsNullOrEmpty(r.ToString()))
{
GlobalVariables.GrantDeny = 1;
}
else
{
throw new System.ArgumentException("Null");
}
}
}
if (GlobalVariables.GrantDeny != 1)
{
Response.Redirect("PageNotAccessable.aspx");
}
}
我尝试将我的“r”foreach语句中的每个“Var”转换为string或int但仍然没有捕获null值尝试了抛出异常它也不会跳过如果它的null它会将页面停止为错误视图。请帮忙..
答案 0 :(得分:1)
你应该先检查IsNull:
public void GrantAccesOnPage(int AccessPoint1, int AccessPoint2)
{
GlobalVariables.GrantDeny = 0;
string[] arr = {
GlobalVariables.SessionPort1,
GlobalVariables.SessionPort2,
GlobalVariables.SessionPort3,
GlobalVariables.SessionPort4
};
foreach (var r in arr)
{
if (string.IsNullOrEmpty(r))
{
//remove the comment out only if you want to throw the exception.
//throw new System.ArgumentException("Null");
}
else
{
if (Convert.ToInt32(r) == AccessPoint1 || Convert.ToInt32(r) == AccessPoint2)
{
GlobalVariables.GrantDeny = 1;
}
}
}
if (GlobalVariables.GrantDeny != 1)
{
Response.Redirect("PageNotAccessable.aspx");
}
}