[英]Catching an exception
我當前正在引發異常,它給出的消息是Value不在預期范圍內。 我正在嘗試糾正一段代碼以捕獲此異常並消除它-我知道問題所在-本質上是有人試圖使用不存在的ID從列表中提取記錄。
任何想法我如何去抓住這個?
要抑制異常,您需要執行以下操作
try
{
// Code that may throw an exception.
}
catch (Exception ex) // Better to use a more specific exception class
{
// Do nothing - That suppresses the exception.
// If you want to do additional checking that may continue the exception
// up the stack use "throw" on its own - which compiled to CIL/MSIL's
// "rethrow" and doesn't drop much of the information that would
// go if you did "throw ex"
}
這就是抑制異常的全部方法。
為了使那些必須維護此代碼的人(或者您自己在六個月的時間內忘記了執行此操作的詳細信息的時間)保持理智,也可以准確地注釋出您為什么抑制該異常。 如果我看到抑制異常的代碼,我總是想知道為什么。
使用帶有空catch
的try
catch
塊嗎?
如果您想變得非常具體,可以使用異常過濾器僅捕獲這種情況(當然在c#6中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.