我已經解決了這個問題,我發佈了。區分原始System.Exception並拋出新的Exception/ApplicationException?
對於瘋狂調用的方法,是否可以區分原始System.Exception(即throw ex;
)和新的Exception(即throw new Exception("Specific error", ex);
)還是新的ApplicationException
?
public void InsertNewCar()
{
try
{
Car myCar = new Car();
myCar.Insert();
}
catch (Exception ex)
{
if (/* This ex is the New Exception */
alert(somethingMissingMsg);
}
else /* This is the original exception */
{
alert(Something wrong generic error);
}
}
public void Insert()
{
try
{
SqlHelper.ExecuteNonQuery(ConnString, CommandType.Text, sqlInsert);
}
catch (SqlException ex)
{
if (ex.Number == 515)
{
throw new Exception("Missing something", ex);
//throw new ApplicationException("Missing something", ex);
}
else
{
throw ex;
}
}
}
謝謝。
[訂購catch塊時,試圖處理異常(可能的重複http://stackoverflow.com/questions/15609597/order-catch-blocks-when-try-to-handle-an-exception )@Stephen – Stephen
,這不是重複的。訂購catch塊如何與此相關?我的問題與實際異常無關,但如何區分接收異常的方法中的異常。 – rbhat
我剛纔明白你的問題....閱讀對方的回答評論後... – Stephen