c#中异常捕获,回滚

语法:

try

{

有可能出现错误的代码写在这里

}

catch

{

出错后的处理

}

如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容,

如果try中的代码一但出错,程序立即跳入catch中去执行代码,那么try中出错代码后的所有代码就不再执行了.

例如:

Ajax:

 1   public void getCancel(HttpContext context)
 2     {
 3         int flag = 0;
 4         try
 5         {
 6             string SONO = context.Request.QueryString["SONO"];
 7             SqlParameter[] parms = {
 8                 new SqlParameter("@SONO", SqlDbType.NVarChar,30){Value=SONO},
 9                 new SqlParameter("@USERID",SqlDbType.NVarChar,16){Value=SysSession.UserId},
10                 new SqlParameter("@RESULT", SqlDbType.Int){Direction=ParameterDirection.Output}               
11             };
12             DBAccess.ExecuteNonQueryProcOut("sp_SO_Delete", parms);
13             string STATUS = parms[2].Value.ConvertObjToStr().Trim();
14             string json = JsonConvert.SerializeObject(STATUS);
15             context.Response.Write(json);
16         }
17         catch (Exception err)
18         {
19             flag = 1;
20             string json = JsonConvert.SerializeObject(flag);
21             context.Response.Write(json);
22         }
23         
24     }