我检测到无法访问的代码'下面的示例代码中出现错误。请帮我解决这个问题与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
}
答案 0 :(得分:7)
这是由于try
和catch
块中的返回,catch块之后的代码永远不会有机会被执行。您可以在UpdateCommentStatus
调用后移动try块中的返回值,或者在UpdateCommentStatus
块中的return语句之前移动try
。
如果成功执行,您可以返回空字符串,如果出现错误,则返回异常消息。您可以考虑以下选项来返回错误。