Error handling is the most common and important part of any web, desktop or any other software application, no matter how proficient the developer is who developed it. Your application throws an exception when some part of the code is not executing as you expected or the users may enter some wrong input or network related issue.
Exceptions(errors) are like a bacteria and a developer can not run away from them. So what to do? Simple: Handle every kind of exception keeping the user experience in mind. So let’s come up with how to handle exceptions in a good and not so good way.
1. try ....catch:
It is a very simple and easy way, just put try catch block where you want to handle your exception into your action method ,any class method and properties etc. In Catch block , you can do whatever you want with exceptions, you can log it, email to admin, or just swallow it. Below code illustrates it,
- public ActionResult Index()
- {
- try
- {
-
- return View();
- }
- catch (Exception ex)
- {
-
- return View("Error");
- }
- }
2. Override OnException method:
Inthe previous section I discussed about the try ....catch technique for code level exception handling.It means that you need to find out all places into your project that an exception can be thrown. This is not a good way. It is very hard work to write try catch everywhere that code can throw exceptiosn. So now let's try to make some reusable code. Now we will try to use base class to make reusable code on controller level.
- protected override void OnException(ExceptionContext filterContext)
- {
- Var lastException = filterContext.Exception;
-
- filterContext.ExceptionHandled = true;
- filterContext.Result = new ViewResult() { ViewName = "Error" };
- }
Now, we can handle all exceptions in controller and we can also send custom error view to browser if any exception occurred. But this is having some limitation e.g.
- It handles only controller level exception.
- If any exception occurred then it response HTTP 200 when it is rendering error view, and also search engine can also crawl this, hence it is also not a good idea.
So what to do, let's think about some more generic way to handle exception
e.g. Global Exception handling.
3. Exception Handling using HandleErrorAttribute:
To achieve global level exception handling , now we need to do some modification into our we.Config file. In System.Web.Section of web.config , we need to add one more section customErrors Section and then add the name of controller (which have error Action)with returning error view or you can also give any HTML page for error. Here we are giving instruction to asp.net that capture all unhandled exceptions and redirect to our custom error page, which we already set into we.config. Here we can also capture individual http error and redirect to any specific custom error page. But still this is not a good solution because using this we can not do some custom processing . So now lets Explore the feature of MVC , known as HandleErrorAttribute action filter.
HandleErrorAttribute will work when custom error mode on in web.config file. When you are using this then make sure that you on custom error. In web.config. E.g.
- <system.web>
- <customErrors mode="On"></customErrors>
- </system.web>
By using HandleErrorAttribute we can handle error at Action level, Controller level and Global level. Lets discuss basic properties of HandleErrorAttribute:
Properties of HandleErrorAttribute:
- ExceptionType: By using we can handle specific exception.If we do not specified it then it will handle all the exceptions.
- View: It mean Name of view which will display the information of exception when exception occurred.
- Master: This property is use to set Master view of displaying exception message.
- Order: It means execute filters by priority . It have integer value and 1 indicate highest priority.
- AllowMultiple: This is used when we want to create multiple object of error filter.
There are some of the way to handle execption using HandleErrorAttribute.
a. Action Level Exception Handling using HandleError:
If you want to handle exceptions on Action level using HandleError attribute simply Add HandleError attribute on you action method with its optional properties. E.g.
- [HandleError(ExceptionType = typeof(DivideByZeroException), View = "dividbyzeroErrorView")]
- public ActionResult Index(int id)
- {
- var result = id / 0;
- return View("Index", result);
- }
In the above example, at the run time when database related exception means System.Data.DataException has occurred then MVC will redirect dbErrorView.If you want to handle all exception then use only HandleError without its property.
- [HandleError]
- public ActionResult Index(int id)
- {
- var result = 1d / 0;
- return View("Index", result);
- }
In Above example the HandleError will handle all types of exceptions which will occurr during index method execute and customerrors mode on with defaultRedirect into our web.config file e.g.
- <system.web>
- <customErrors mode="On" defaultRedirect="error.html"></customErrors>
- </system.web>
b. Controller Level Exception Handling:
As with Action level exception we can also handle exceptions at controller level which means if error occurred in any of the action methods of this controller then HandleError filter will call and Handle error. Handling controller level exception is pretty easy just decorate you controller with HandlerError Attribute according to your requirement. e.g.
- [HandleError(ExceptionType = typeof(DivideByZeroException), View = "DividByZeroError")]
- [HandleError(ExceptionType=typeof(NullReferenceException),View= "CustomErrorPage")]
- public class HomeController : Controller
- {
-
- }
It has a limitation that we need to decorate each controller of our application where we want to handle exception using HandleError Attribute.
c. Global Level Exception Handling:
In previous points we handle exception on Controller level. But if I want to handle exception globally then what to do , Its simple We need to add HandleError attribute in FilterConfig.cs . We can find FilterConfig.cs in App-Start folder of our MVC Application.
- public class FilterConfig
- {
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- }
- }
In above code FilterConfig class contains one static method name RegisterGlobalFilters which is have one argument GlobalFilterCollection as input and now we added HandleError Attribut in it.in Global.asax file invoked this method . e.g.
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
-
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
Now it will handle exception of all controller but this is still have limitations.
- We can not exception log or email to Admin.
- When error occurred out side the controller then It will not catch exception.
- At Ajax request time , If any exception occurred then its will return error view , which is not a good things.
So finally the solution I like out of all this problems are extending HandleError Attribute.
d. Extending HandleErrorAttribute:
Let's Create a generic error Handling attribute which handle all exception as well log error into database or mail to admin according to your requirement or return JSON error object for Ajax call if any exception occurred. So lets dive into code without boring you much.
-
- public class CustomErrorAttribute : HandleErrorAttribute
- {
-
- public override void OnException(ExceptionContext filterContext)
- {
-
- if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
- {
- return;
- }
- var statusCode = (int)HttpStatusCode.InternalServerError;
-
- if (filterContext.Exception is HttpException)
- {
- statusCode = new HttpException(null, filterContext.Exception).GetHttpCode();
- }
-
-
-
- if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
- {
- filterContext.Result = new JsonResult
- {
- JsonRequestBehavior = JsonRequestBehavior.AllowGet,
- Data = new
- {
- error = true,
- message = filterContext.Exception.Message
- }
- };
- }
- else
- {
- var controllerName = filterContext.RouteData.Values["controller"].ToString();
- var actionName = filterContext.RouteData.Values["action"].ToString();
- var errormodel = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
-
- filterContext.Result = new ViewResult
- {
- ViewName = View,
- MasterName = Master,
- ViewData = new ViewDataDictionary(errormodel),
- TempData = filterContext.Controller.TempData
- };
- }
-
-
-
-
-
-
-
-
- filterContext.ExceptionHandled = true;
- filterContext.HttpContext.Response.Clear();
- filterContext.HttpContext.Response.StatusCode = statusCode;
-
- filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
- }
- }
Conclusion:
Exception handling is an important part of any Software application. So now by extending HandleErrorAttribute we can play with exceptions in asp.net MVC. Your comments, feedback, suggestions and critique are welcomed.
Read more articles on ASP.NET: