Try-Catch巢狀犯的錯誤
阿新 • • 發佈:2019-02-19
最近在寫一個小程式時用到了巢狀Try-Catch,個人對C#沒什麼理解,只是隨手拿來用的,所以犯了個錯誤,記錄一下
原來的程式碼:
class Program { static void Main(string[] args) { while(true) { try { AOIFileMonitor(); } catch (System.Exception e) { File.AppendAllText("D:\\FileMonitor.log", DateTime.Now.ToString() + "," + e.Message.ToString() + "\r\n",Encoding.Default); } <span style="white-space:pre"> </span>thread.sleep(2000);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private static void AOIFileMonitor()
{
<span style="font-family: Arial, Helvetica, sans-serif;">string strSql="insert .....";//插入資料到一個有主鍵的表中</span>
try
{
ClientUtils.ExecuteSQL(strSql);
}
catch(SystemException e)
{
strSql="Update....";
ClientUtils.ExecuteSQL(strSql);
}
}
}
</pre>程式中的Update的sql總是無法執行,在insert的sql發生錯誤的時候直接跳到了外層的catch塊中。後來把內層的<pre name="code" class="csharp">catch(SystemException e)
改成單純的catch才能正常執行。