2016-08-19 59 views
1

我收到unreachable code警告catch catch並且我無法調試。請建議我如何糾正這種錯誤:在C#.Net中檢測到無法訪問的代碼.net

private void HandleDevelopmentServer() 
{ 
    string sErrMsg = ""; 
    try 
    { 
     QuasarInterfaces.ISecurity oSecurity = null; 
     Global.CreateSecurityComponent(ref oSecurity); 
     System.Data.DataSet oDS; 
     DataTable dtDBSettings = new DataTable(); 
     string sDBString = System.Configuration.ConfigurationSettings.AppSettings["Environment"]; 
     Global.ReadDBConfig(sDBString, ref dtDBSettings); 
     oSecurity.FetchAllUsers(out oDS, out sErrMsg, dtDBSettings) 

     if (sErrMsg.Length > 0) 
      throw new Exception(sErrMsg);    

     if ((oDS != null) && (oDS.Tables.Count != 0)) 
     { 
      DropDownList1.DataSource = oDS; 
      DropDownList1.DataBind(); 
     } 
    } 
    catch (Exception e) 
    { 
     throw new Exception("HandleDevelopmentServer function failed;" + e.Message);    
     Global.writeLog("" + e.ToString()); 
    } 
} 
+2

在拋出異常之前移動日誌記錄。 –

+1

嗯,在你拋出異常之後,不要嘗試記錄*。您如何期待'Global.writeLog'方法調用能夠工作? (你應該學習.NET命名約定,順便說一句)。 –

+1

正如錯誤信息中所述,'Global.writeLog(「」+ e.ToString());'部分是不可訪問的,因爲你之前沒有拋出異常處理它。 –

回答

4

這條線將永遠不會發生:

Global.writeLog("" + e.ToString()); 

你拋出一個異常,它上面,這意味着該方法將在此時退出到以前的調用堆棧

只需切換兩個就沒問題。

catch (Exception e) 
{ 
    Global.writeLog("" + e.ToString()); 
    throw new Exception("HandleDevelopmentServer function failed;" + e.Message);    
} 

而且你還可以去除"" +

+0

@Ganesh Chemanchula - 這有助於你解決問題嗎? –

+0

是吉拉!非常感謝:) –

+0

@GaneshChemanchula - 請考慮通過接受我的回答標記問題已解決 –

2

您必須切換catch塊中的兩行,以便在拋出異常之前寫入日誌。