【问题标题】:Which is correct way to check for Null exception?检查 Null 异常的正确方法是什么?
【发布时间】:2008-10-03 02:41:44
【问题描述】:

哪个是最正确的代码?

if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
    HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
}

if (HttpContext.Current != null)
    if (HttpContext.Current.Response != null)
        if (HttpContext.Current.Response.Cookies != null)
            if (HttpContext.Current.Response.Cookies[authCookieName] != null)
                HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果 HttpContext、HttpContext.Current、HttpContext.Current.Response 或 Http.Current.Response.Cookies 中的任何一个为 null,那么您已经有麻烦了。让异常发生并修复您的网络服务器。

    【讨论】:

    • 顺便说一句:HttpContext 是一个类。如果一个类是空的,你肯定有麻烦了! :P
    【解决方案2】:

    两者都很好。假设您已经检查了需要首先检查的所有其他内容。例如:

    private bool CheckSuspendersAndBelt()
    {
        try
        {
            //ensure that true is true...
            if (true == true)
            {
                //...and that false is false...
                if (false == false)
                {
                    //...and that true and false are not equal...
                    if (false != true)
                    {
                        //don't proceed if we don't have at least one processor
                        if (System.Environment.ProcessorCount > 0)
                        {
                            //and if there is no system directory then something is wrong
                            if (System.Environment.SystemDirectory != null)
                            {
                                //hopefully the code is running under some version of the CLR...
                                if (System.Environment.Version != null)
                                {
                                    //we don't want to proceed if we're not in a process...
                                    if (System.Diagnostics.Process.GetCurrentProcess() != null)
                                    {
                                        //and code running without a thread would not be good...
                                        if (System.Threading.Thread.CurrentThread != null)
                                        {
                                            //finally, make sure instantiating an object really results in an object...
                                            if (typeof(System.Object) == (new System.Object()).GetType())
                                            {
                                                //good to go
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
        catch
        {
            return false;
        }
    }
    

    (对不起,无法抗拒...... :))

    【讨论】:

      【解决方案3】:

      可以试试:

      if(HttpContext.Current != null && 
         HttpContext.Current.Response != null && 
         HttpContext.Current.Response.Cookies != null && 
         HttpContext.Current.Response.Cookies[authCookieName] != null) 
      {
          // do your thing
      }
      

      【讨论】:

        【解决方案4】:

        HttpContext.Current.Response.Cookies 永远不会为空。唯一可能导致 null 的是,如果您期望的 cookie 不存在,那么第一个是正确的。如果您不接受网络请求,HttpContext.Current 将为 null :)

        【讨论】:

          【解决方案5】:

          你给出的第一个例子已经足够了。如前所述,如果任何其他对象为空,则 ASP.NET 存在问题。

          if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
              HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
          }
          

          但是,您应该创建一些通用函数,例如 SetCookieGetCookieGetQueryStringGetForm 等接受名称和值(对于 Set 函数)作为参数,处理 null 检查,并返回值或空字符串(对于 Get 函数)。这将使您的代码更易于维护并可能改进,如果您决定在未来使用 Cookie 以外的东西来存储/检索选项,您只需更改功能。

          【讨论】:

            【解决方案6】:

            两者都不是真的更正确,尽管我会避免使用第二种,因为深度嵌套的条件往往难以理解和维护。

            如果您希望得到一个空指针异常,请使用第一个。如果您想以其他方式或静默处理 null,请使用第二种方式(或第二种方式的重构版本)。

            【讨论】:

              【解决方案7】:

              如果您认为 CurrentResponseCookiesCookies[authCookieName] 有可能是 null,并且如果其中任何一个是,那么您有合理的事情要做,那么后者就是要走的路。如果机会很低,和/或如果中间体为空,您无能为力,请选择前者,因为它更简洁 - 如果您使用扩展示例,您可以做的最好的事情是获得更好的日志记录。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-03-20
                • 2012-04-05
                • 2013-08-31
                • 2016-09-03
                • 2012-10-16
                • 2012-02-17
                • 2012-03-29
                • 1970-01-01
                相关资源
                最近更新 更多