2016-08-19 62 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块中的两行,以便在抛出异常之前写入日志。