在c#中检测到无法访问的功能代码

时间:2014-05-29 07:28:58

标签: c# asp.net

我检测到无法访问的代码'下面的示例代码中出现错误。请帮我解决这个问题与try和catch语句有什么关系吗?函数UpdateCommentStatus在同一个cs文件中:

public string insertEmail(string pstrEmailFrom, string pstrEmailTo, string pstrEmailSubject, string pstrEmailBody, string pstrBRID, string pstrTicketID, int include_Attachment)
    {
        // Include Attachment is for keep track of only first email notification will contain attachment.
        // This is done to conserve bandwidth and processing. 
        string strSQL;
        string strEmailSubject = pstrEmailSubject.Replace("'", "''");
        string strEmailBody = pstrEmailBody.Replace("'", "''");
        strSQL = "INSERT INTO CRM_EMAIL(email_to,email_from,email_subject,email_body,created_date,";
        strSQL = strSQL + " br_id,notes,status,ticket_id, INCLUDE_ATTACHMENT,SEG_ID) VALUES ";
        strSQL = strSQL + "('" + pstrEmailFrom.Replace("'", "") + "','" + pstrEmailTo.Replace("'", "") + "','" + strEmailSubject + "', ";
        strSQL = strSQL + "'" + strEmailBody + "',NOW(), ";
        strSQL = strSQL + "'" + pstrBRID + "','','N', '" + pstrTicketID + "', " + include_Attachment + ",'" + mag.getSegID() + "')";
        mag.WriteToNormalLogFile("insertEmail()  strSQL:" + strSQL);
        try
        {
            objDBinterface.strConn = mag.ConnStr();
            objDBinterface.ExecSQL(strSQL);
            UpdateCommentStatus(pstrTicketID);
            return "";
        }
        catch (Exception ex)
        {
            mag.WriteToLogFile("insertEmail : " + ex.ToString());
            return ex.ToString();
        }

        UpdateCommentStatus(pstrTicketID); <<-- HERE
    }

1 个答案:

答案 0 :(得分:7)

这是由于trycatch块中的返回,catch块之后的代码永远不会有机会被执行。您可以在UpdateCommentStatus调用后移动try块中的返回值,或者在UpdateCommentStatus块中的return语句之前移动try

如果成功执行,您可以返回空字符串,如果出现错误,则返回异常消息。您可以考虑以下选项来返回错误。

  • 使返回类型为bool并抛出一个execption。
  • 返回bool并使用out参数传达错误。