[英]How to convert System.Exception into FaultException
我有以下異步調用的方法。 我想知道如何將System Exception
(在SQL操作期間可能會遇到)轉換為FaultException
。
這是方法:
public List<Product> GetProductDetails(int productKey)
{
try
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("usp_Get_ProductDetails", con);
........
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
......
}
}
}
}
catch(Exception ex)
{
//How can I convert this Exception ex into FaultException and then throw to client?
//throw new FaultException(new FaultReason(new FaultReasonText()), new FaultCode());
}
}
您可以捕獲多個異常(如果合理地發生給定的異常,這樣做實際上是一個好習慣),然后對每個異常進行處理。
例如:
try
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("usp_Get_ProductDetails", con);
.....
}
}
catch(FaultException faultEx)
{
// code to send to client
}
catch(Exception ex)
{
// code to do something else
}
catch(Exception ex)
{
throw new FaultException(ex.Message);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.