This article helps to track unhandled exception in MVC with Exception Filter without termination of the application.
Introduction
In projects, exception is a common thing to handle in
application. Let’s try to first understand what an Exception and what exception
can do in the project. Exception is
nothing but error which comes when program does not handle the rules of the
program. Exception can occur at any time. It could be the reason of server failing,
session timeout, database failure and hardware failure. Exception prevents the
termination of the program when program or code does not recognize the error
and redirect the exact error message to the browser or display the error page
to the client to understand what error throws.
Overview of Exception
The screen shot shows the how unhandled exception occurs. When
client sends a request to the server. Browser displays the errors which are not
recognized by the program.

Classification of Action Filters in MVC.
We will discuss now about Custom action
filter types in MVC. There are four types of Filters in ASP.NET MVC as below
described-
Authorization
Filters: It is responsible for checking user access. In the below code Authorize attribute is used
to prevent the Contact action method to access by administrator only. We can
allow anonymous access to the action methods of the controller-
[Authorize(Roles="Administrator")]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
Action Filters: It is used to access the action before and
after the execution using override methods.
In the below code override method of Action filter is used to trace the
action methods information. Two Override methods: OnActionExecuted, OnActionExecuting.
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.ExceptionHandled = true;
}
Result Filters: It is used to access
the log information of action result methods. Two override methods- OnResultExecuted,OnResultExecuting. Mainly OutputCacheAttribute
is used in
Result filter.
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.ExceptionHandled = true;
}
Exception
Filter: - It is used to handle the unhandled anomalies exception at runtime.
We will discuss more on Exception filter and will see an example as well.
Let s start to implement
the Exception filter. We will create a sample project.
File > New>
Project> Select MVC application.
Added below code to
handle the exception. filtercontext is passed as a parameter to enable the
exception. We are re-directing the Error page once exception occurs.
protected override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult("~/Home/ErrorMessage");
base.OnException(filterContext);
}
Let’s see what will happen if
we will not implement the override OnException method in the Home Controller,
see below screen shot.

In the below screen shot
shows how user encounter the error when any exception found. We have created a new action methods and
override the OnException method in Home controller.

Now we create new ErrorMessage action method which returns
the Error page from View. Error Page will be available by default in the shared
folder in the MVC 5 template.
public ActionResult ErrorMessage()
{
return View("Error");
}
The below code, we tried to raise the exception when user
hits the contact action method.
public ActionResult Contact()
{
throw new Exception("Exception occurred");
}
Exception Filter Code Snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace JkExceptionFilterDemo.Controllers
{
public class HomeController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult("~/Home/ErrorMessage");
base.OnException(filterContext);
}
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
throw new Exception("Exception occuured");
}
public ActionResult ErrorMessage()
{
return View("Error");
}
}
}
After override the OnException method in the code, it shows
the proper error message to the client and he can come back to Home page after
click on Home Page without terminating the program.

When we click on Home page
and then we will see Home page of the
appliation. And here we have handled the un recognized exception at the
runtime. So we have done the Exception Filter in MVC.

Conclusion
Here we have discussed on how unhandled
exception is caught and displayed to the client to better understanding what
actually happened in the application. I have attached the Exception filter demo
class for better understanding the code structure of the application. Thanks for the reading.