Exception or Error Handling in ASP.Net MVC Using HandleError Attribute

I am using kendo Grid for 4 years , and i find the built-in edit template the best to show edit form.

Introduction

Exception handling may be required in any application, whether it is a web application or a Windows Forms application.

ASP.Net MVC has an attribute called "HandleError" that provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as Controller or at the global level. The HandleError attribute is the default implementation of IExceptionFilter. When we create a MVC application, the HandleError attribute is added within the Global.asax.cs file and registered in the Application_Start event.

  1. public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
  2. {  
  3.     filters.Add(new HandleErrorAttribute());  
  4. }  
  5.   
  6. protected void Application_Start()  
  7. {  
  8.     AreaRegistration.RegisterAllAreas();  
  9.     RegisterGlobalFilters(GlobalFilters.Filters);  
  10.     RegisterRoutes(RouteTable.Routes);  
  11. }
Important properties of HandleError attribute

The HandleError Error attribute has a couple for properties that are very useful in handling the exception.

ExceptionType: Type of exception to be catch. If this property is not specified then the HandleError filter handles all exceptions.

View: Name of the view page for displaying the exception information.

Master: Master View for displaying the exception.

Order: Order in which the action filters are executed. The Order property has an integer value and it specifies the priority from 1 to any positive integer value. 1 means highest priority and the greater the value of the integer is, the lower is the priority of the filter.

AllowMultiple: It indicates whether more than one instance of the error filter attribute can be specified.

Example
  1. [HandleError(View = "Error")]  
  2. public class HomeController : Controller  
  3. {  
  4.     public ActionResult Index()  
  5.     {  
  6.         ViewBag.Message = "Welcome to ASP.NET MVC!";  
  7.         int u = Convert.ToInt32("");// Error line  
  8.         return View();  
  9.     }  
  10. } 
HandleError Attribute at Action Method Level

  1. [HandleError(View = "Error")]  
  2. public ActionResult Index()  
  3. {  
  4.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  5.     int u = Convert.ToInt32("");// Error line  
  6.     return View();  
  7. }

Defining HandleError Attribute at Global Level

We can also apply the HandleError Attribute at the global level by registering it in the Global.asax in Application_Start event.

Example

  1. public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
  2. {  
  3.     filters.Add(new HandleErrorAttribute(), 2); //by default added  
  4.   
  5.   
  6.     filters.Add(new HandleErrorAttribute  
  7.     {  
  8.         View = "Error"  
  9.     }, 1);  
  10. }  
  11. protected void Application_Start()  
  12. {  
  13.     AreaRegistration.RegisterAllAreas();  
  14.     RegisterGlobalFilters(GlobalFilters.Filters);  
  15.     RegisterRoutes(RouteTable.Routes);  
  16. } 

Error Page

  1. @model System.Web.Mvc.HandleErrorInfo    
  2. @{    
  3.     ViewBag.Title = "Error";    
  4. }    
  5. <h2>    
  6.     Sorry, an error occurred while processing your request.    
  7.     <br />    
  8.     Error :    
  9. </h2>    
  10. <p>    
  11.      @Model.Exception    
  12. </p> 

Enabling Custom Error Handling

To enable custom error handling in the application by the HandleError attribute, we must add a CustomErrors element within the system.web section of web config.

  1. <system.web>  
  2.   <customErrors mode="On" defaultRedirect="Error" />  
  3.    ...  
  4.    ...  
  5. <system.web>

Output

Catch Specific Type of Error

The ExceptionType property can help us to catch a specific type of error. For example, if we want to catch only an ApplicationException type of exception then we can define the ExceptionType.

  1. [HandleError(ExceptionType = typeof(ApplicationException), View = "Error")]  
  2. public ActionResult Index()  
  3. {  
  4.     ViewBag.Message = "Welcome to ASP.NET MVC!";  
  5.     int u = Convert.ToInt32(""); // Error line  
  6.     return View();  
  7. }

Limitation of HandleError attribute

  • It does not catch a HTTP exception other than 500 and bypasses all the others.
  • It is not able to catch an exception raised outside of controllers.
  • The logging of an exception is not supported by the HandleError attribute.
  • It returns an error view even if the error occurred in an AJAX client call.

Summary

The HandleError exception Filter can be applied over the action method or on the controller even if at the global level. An Exception Filter is implemented from the IExceptionFilter interface and it calls the OnException method when the exception occurs. Finally, using this attribute, we can handle exceptions with some limitations.


Similar Articles