Exception Handing Using HandleError Attribute In ASP.NET MVC

It is a main concern for all the developers how to handle error or exception in application. As you know the application can be a desktop application or web application. Basically as you all know for exception handling we use Try-Catch-Finally block. Inside the Catch block, we catch the different types of exception and show the relevant and user friendly information to user.

Today’s topic is how to handle the exception in ASP.NET MVC using HandleError attribute. In ASP.NET MVC, there are different ways to handle the exception. Generally we can use Try-Catch or some filters are available to handle the exception or sometimes we use configuration settings to show the relevant page when exception occurs.


What is HandleError Attribute?

It is a type of filter and works when you have enabled the custom errors in web.config. Sometimes, it is required to show the custom error using the custom page and then we can use HandleError Attribute for exception handling. It only works for error status code 500. You can enable Custom Error to add customErrors attribute inside your configuration setting within the system.web node.


You can use [HandleError] attribute with your action to handle the exception at action level or with your controller to handle the exception at controller level or it can also be global level.

If you are going to use [HandleError] attribute, it means this error is applicable only for this action. First you need to decorate the action with this attribute, needs to be provide some other details like type of the exception and your View name.

General Syntax

  1. [HandleError]  
  2. public ActionResult Index()  
  3. {  
  4.     return View();  
  5. }  
  6. Or[HandleError]  
  7. public class HomeController: Controller  
  8. {}  

Implementing the HandleError attribute with action
  1. [HandleError(ExceptionType = typeof(System.Data.DataException), View = "BlogError")]  
  2. public ActionResult Index(int articleId)  
  3. {  
  4.     var db = new BlogContext();  
  5.     return View("Index", db.Articles.Single(x => x.ArticleId == articleId));  
  6. }  
You can see in above code snippet that we have decorated the Index action with HandleError attribute and also defined the type of error. It is also shown that if error occurs then which view should be shown.

Implementating the HandleError attribute with controller
  1. [HandleError(ExceptionType = typeof(System.Data.DataException), View = "CommonBlogError")]  
  2. public class HomeController: Controller  
  3. {  
  4.     // It will work for all the action method inside the Home controller  
  5. }  
If you are going to handle the exception at controller level, there is no need to do anything only you need to decorate the HandleError attribute with your controller and also provide the name of the common view.

You can also add different views for different types of the exception as in the following to decorate multiple HandleError attribute.
  1. [HandleError(ExceptionType = typeof(System.Data.DataException), View = "CommanBlogError")]  
  2. [HandleError(ExceptionType = typeof(NullReferenceException), View = "NullError")]  
  3. public class HomeController: Controller  
  4. {  
  5.     /* Controller Actions with HandleError applied to them */  
  6. }  
Implementing HandleError at global level

Sometimes, you need to handle error at global level for entire application and show the custom error page or view. So, to add the error globally, you need to add the error with RegisterGlobalFilters() which is inside the FilterConfig.cs where we can define all the filters settings.

When you create a new application in ASP.NET MVC then it auto adds the FilterConfig.cs inside the App_Start folder and also adds HandleErrorAttribute here.

Here you can add your custom filter into the global filter collection where you can define the type of the exception and view needs to be shown.
  1. using System.Web;  
  2. using System.Web.Mvc;  
  3. namespace TestCasesDemo  
  4. {  
  5.     public class FilterConfig  
  6.     {  
  7.         public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
  8.         {  
  9.             filters.Add(new HandleErrorAttribute  
  10.             {  
  11.                 ExceptionType = typeof(System.Data.DataException),  
  12.                     View = "CommonBlogError"  
  13.             });  
  14.             filters.Add(new HandleErrorAttribute());  
  15.         }  
  16.     }  
  17. }  


There are some limitations of the HandleError Attribute:
  1. It does not log the error anywhere; you need to do it manually with some other code.
  2. It does not handle the error which is not occurring on controller or action level.
  3. It only handles HTTP 500 status error, if any other HTTP code error generates then it will not handle by HandleError attribute.
  4. It also returns only view when you are making the ajax call at client side, which is not reliable.

Thanks for reading this article, hope you enjoyed it.