最近有点疑惑,异常捕获该如何使用呢?
我习惯于
try{}
catch(SystemException se)
{
;
}
这样的用法。catch 快里面基本不使用se。也就是说,catch主要防止程序down掉。
看了一下最近的一个项目,总共有180多个warning,都是类似 The variable 'se' is declared but never used 的。
希望大家都来谈谈使用try catch的体会……。也希望高手能给予指点。
因为是论坛新人,总共只有20分,都拿出来了。希望大家不要见怪。
我习惯于
try{}
catch(SystemException se)
{
;
}
这样的用法。catch 快里面基本不使用se。也就是说,catch主要防止程序down掉。
看了一下最近的一个项目,总共有180多个warning,都是类似 The variable 'se' is declared but never used 的。
希望大家都来谈谈使用try catch的体会……。也希望高手能给予指点。
因为是论坛新人,总共只有20分,都拿出来了。希望大家不要见怪。
。。
}
catch {}
顶一楼的
。。
}
catch {}
这种写法虽然避免了表面的错误,但是有可能隐藏致命的错误。不推荐。
catch
{
throw;//继续抛出异常不可省略
}
只是因为底层不需要处理异常,继续抛出让上层去处理...以此类推...
http://topic.csdn.net/u/20090423/11/aa4e511c-d6bc-4328-9aa2-a8141d5d8429.html
try{
}
catch(ArgumentNullException e)
{
//可处理的异常
}
catch
{
//未处理的异常
throw new Exception();
}
finally
{
//Executing finally block.
}
}
catch(ArgumentNullException e)
{
//仅处理可处理的异常
}
catch
{
//未处理的异常,交由上层处理(如ASP.NET 错误友好页 )
throw;
}
finally
{
//Executing finally block.
}
或是
catch(SystemException se){
Debug.WriteLine(se);
}
{}
catch(Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "系统提示");
}
一直这样用着,好像没有被warning
你自己做点小程序或者无虞,做大程序万一发现结果有问题,一查,满程序都是catch{},你去哪找BUG?
{
using (DataAccess DataAcc = new DataAccess())
{
orderCode = DataAcc.GetCode((int)MemberCodeType.委托单号);
dtAdd.Rows[0].SetField<string>("order_code", orderCode); if (dtAddList != null)
{
foreach (DataRow row in dtAddList.Rows)
{
row.SetField<string>("order_code", orderCode);
}
} ids = DataAcc.InsertData(dsAdd);
}
if (ids != null && ids.Length > 0 && ids[0].ListID.Length > 0)
{
this.AffectedCode = ids[0].ListID[0];
return ids[0].ListID[0];
}
return -1;
}
catch (System.ServiceModel.FaultException<string> ex)
{
string addtional = "方法" + ex.Reason.ToString() + ";" + ex.Detail;
throw FaultNotificationException.GetFaultNotificationException("新增委托单时出错",
"WangDa.ExBusiness.Consignor.Order.Add方法",
addtional, null);
}
catch (Exception ex)
{
throw FaultNotificationException.GetFaultNotificationException(ex.Message,
"WangDa.ExBusiness.Consignor.Order.Add方法",
"", ex);
}
finally
{
if (dtAdd != null) dtAdd.Dispose();
if (dtAddList != null) dtAddList.Dispose();
}