Normally, I don't anticipate exceptions and if I get them, its something that I can't fix in my code - bad user input or connectivity issues with the database.
But errors do occur so at the very least, I want to inform my user that something went wrong and log the error.
My exception handling generally looks something like this:
public JsonResult Create(UserCreateEditViewModel model) { try { if (!ModelState.IsValid) return Json(new { IsOk = false, Message = "Some of your data was invalid, please try again." }); var User = new User { // map UserCreateEditViewModel properties to User properties }; OrtundEntities Db = new OrtundEntities(); Db.Users.Add(User); Db.SaveChanges(); return Json } catch(Exception ex) { // can't correct user data or repair connection // issues to the database so just report the error to the user return ReportError(ex, "Create User"); } } public JsonResult ReportError(Exception ex, string action) { return Json(new { IsOk = false, Title = action, Message = ex.Message }); } I'm aware that in most cases, Pokemon exception handling is discouraged, but since there's nothing really I can do with an error, is Pokemon exception handling not okay to use simply to report the error?
return Jsonin yourtryisn't valid C#.