2008-10-03 52 views
5

哪一個是最正確的代碼?哪個是檢查Null異常的正確方法?

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

回答

19

如果HttpContext的,HttpContext.Current,HttpContext.Current.Response中的任何一個,或Http.Current.Response.Cookies爲空,你已經陷入困境。讓異常發生並修復您的Web服務器。

+0

BTW:HttpContext是一個類。如果一個類是空的,你肯定有麻煩了! :P – yfeldblum 2008-10-03 03:17:39

+0

訣竅問題.... drat! – craigmoliver 2008-10-03 04:36:07

0

雖然我會避免第二個問題,但它們都不是很正確,因爲深嵌的條件往往難以理解和維護。

如果您希望獲得空指針異常,請使用第一個。如果你想以另一種方式或默默地處理空值,使用第二個(或第二個的重構版本)。

0

如果你想有一個機會,CurrentResponseCookies,或Cookies[authCookieName]可能是null,你有一件合理的事做,如果其中的任何一樣,那麼後者的路要走。如果機會很少,並且/或者如果中間體爲空,那麼就沒有什麼辦法可以做,因爲它更簡潔 - 如果您使用擴展示例,最好做的是更好地記錄日誌。

4

可以嘗試:

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

HttpContext.Current.Response.Cookies永遠不會爲空。唯一可能導致null的是如果你期望的cookie不存在,那麼第一個是正確的。 HttpContext.Current將爲null如果你不接受一個web請求通過:)

4

兩者都很好。假設你已經檢查了所有需要首先檢查的其他東西。例如: -

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

(對不起,無法抗拒... :))

1

你給我的第一個例子是綽綽有餘。就像上面提到的那樣,如果其他任何對象都是空的,那麼ASP.NET有問題。

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

但是比起這些往往是許多亂丟檢查你的代碼,你應該創建一些通用的功能,如的setcookie的getCookieGetQueryString,並GetForm等,這些接受的名稱和值(用於設置函數)作爲參數,處理空檢查,並返回值或空字符串(用於獲取函數)。這將使您的代碼更容易維護並可能改進,如果您決定使用除Cookies之外的其他功能來存儲/檢索選項,則只需更改這些功能即可。