What are the Exception filters in MVC?

Category: C SharpWhat are the Exception filters in MVC?
Editor">Editor Staff asked 2 years ago

What are the Exception filters in MVC?

1 Answers
Editor">Editor Staff answered 2 years ago

Exception are part and parcel of an application. They are a boon and a ban for an application too. Isn’t it? This would be controversial, for developers it helps them track minor and major defects in an application and sometimes they are frustrating when it lets users land on the Yellow screen of death each time. This would make the users mundane to the application. Thus to avoid this, developers handle the exceptions. But still sometimes there are a few unhandled exceptions.
Now what is to be done for them? MVC provides us with built-in “Exception Filters” about which we will explain here.
Most Asked ASP.NET MVC Interview Questions and Answers
cc: Google 
Let’s start!
A Yellow screen of Death can be said is as a wardrobe malfunction of our application.
Get Started
Exception filters run when some of the exceptions are unhandled and thrown from an invoked action. The reason for the exception can be anything and so is the source of the exception.
Creating an Exception Filter
Custom Exception Filters must implement the builtinIExceptionFilter interface. The interface looks as in the following,


  1. public interface IExceptionFilter  
  2. {  
  3.     void OnException(ExceptionContext filterContext)  
  4. }  

Whenever an unhandled exception is encountered, the OnException method gets invoked. The parameter as we can see, ExceptionContext is derived from the ControllerContext and has a number of built-in properties that can be used to get the information about the request causing the exception. Their property’s ExceptionContextpassess are shown in the following table:  


The result returned by the action being invoked.

The unhandled exceptions caused from the actions in the applications.

This is a very handy property that returns a bool value (true/false) based on if the exception is handled by any of the filters in the applicaiton or not.

The exception being thrown from the action is detailed by the Exception property and once handled (if), then the property ExceptionHandled can be toggled, so that the other filters would know if the exception has been already handled and cancel the other filter requests to handle. The problem is that if the exceptions are not handled, then the default MVC behavior shows the dreaded yellow screen of death. To the users, that makes a very impression on the users and more importantly, it exposes the application’s handy and secure information to the outside world that may have hackers and then the application gets into the road to hell. Thus, the exceptions need to be dealt with very carefully. Let’s show one small custom exception filter. This filter can be stored inside the Filters folder in the web project of the solution. Let’s add a file/class called CustomExceptionFilter.cs.


  1. public class CustomExceptionFilter: FilterAttribute,  
  2.     IExceptionFilter   
  3. {  
  4.         public void OnException(ExceptionContext filterContext)  
  5.         {  
  6.             if (!filterContext.ExceptionHandled && filterContext.Exception is NullReferenceException)  
  7.             {  
  8.                 filterContext.Result = new RedirectResult(“customErrorPage.html”);  
  9.                 filterContext.ExceptionHandled = true;  
  10.             }  
  11.         }  
  12.     }