Try-Catch嵌套犯的错误

最近在写一个小程序时用到了嵌套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);                     
                     }
		thread.sleep(2000);
		}
	}
	private static void AOIFileMonitor()
        {

string strSql="insert .....";//插入数据到一个有主键的表中

try

{

ClientUtils.ExecuteSQL(strSql);

}

catch(SystemException e)

{

strSql="Update....";

ClientUtils.ExecuteSQL(strSql);

}

}

}


程序中的Update的sql总是无法执行,在insert的sql发生错误的时候直接跳到了外层的catch块中。后来把内层的
catch(SystemException e)
改成单纯的catch才能正常执行。

你可能感兴趣的:(C#)