我使用下面的代碼在我的控制器,當我提出一個新的條目:我怎麼能處理所有可能出現的異常,當我做EF6/SQL Server中的2012年後
// POST /api/Content/
public HttpResponseMessage PostContent(Content content)
{
try
{
content.ModifiedDate = DateTime.Now;
_uow.Contents.Add(content);
_uow.Commit();
var response = Request.CreateResponse<Content>(HttpStatusCode.Created, content);
return response;
}
catch (DbUpdateException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex);
}
}
這只是拿起DbUpdateExceptions所以如果有另一種例外,那麼我認爲我需要以不同的方式處理。
任何人都可以建議我應該如何處理其他例外?
嘗試分離您的圖層,以便您的DataAccess圖層可以捕獲它自己的錯誤,記錄它們並向調用者拋出友好的消息或更一般的異常。從那裏你可以捕捉到這個錯誤,並按照你的選擇做。 –
http://stackoverflow.com/questions/15167927/how-do-i-log-all-exceptions-globally-for-ac-sharp-mvc4-webapi-app – qujck
*任何人都可以建議我應該如何處理其他例外? *處理可以有意義的那些(例如'DbUpdateException'),否則使用[HandleErrorAttribute'](http://msdn.microsoft.com/zh-cn/library/system.web.mvc.handleerrorattribute(v =對108).aspx)的MVC。 –