Exception Handling in MVC 4

Hi, In the below post we are going to learn what are the options provided by MVC which can helps us for exception handling. Exceptions are part of an application so we just need to consider it as we are considering any other stuff of application. Just adding try and catch block for every action is not a good practice. There are different options given by MVC framework for it.

You can use “HandleErrorAttribute” filter attribute just above the action definition in which you can specify the type of exception and the view which you want to display when these types of exception will occurred in your application.

[HandleError(ExceptionType=typeof(System.Data.EntitySqlException),View="SqlExcetion");

The above line will display the “SqlException” view whenever the EntitySql exception will appears. You can add this attribute at action level, controller level as well as application level in the filterConfig file’s “RegisterGlobalFilters” method as below.

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute {
ExceptionType = typeof(System.Data.EntitySqlException),
View = "SqlExcetion" });

filters.Add(new HandleErrorAttribute());
}
}

One thing you should remember that the action filters are executed in the same sequence it defined so if you want you need to take care of sequencing it.

Another option is you can just inherit the “HandleErrorAttribute”  and write your own logic for exception handling . You can add a class in your project as and inherit the “HandleErrorAttribute” as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NorthwindProject.Custom
{
public class MyCustomError:HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// Your custom code for handling Excetion.
base.OnException(filterContext);
}
}
}

After that you can apply this on the action as below.

[MyCustomError(ExceptionType=typeof(System.Data.EntitySqlException),View="SqlExcetion")]

If you don’t want to use the Action filter then you can inherit the base controller “OnException” method and then add the exception handling logic there as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NorthwindProject.Custom
{
public class MyCustomContoller : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//Your custom code
base.OnException(filterContext);
}
}
}

After that you need inherit your applications controllers with the “MyCustomContoller” class in which you want the custom exception handling.

public class CustomerController : MyCustomContoller

 

Hope you find this post useful , thanks for reading.
Advertisements

2 thoughts on “Exception Handling in MVC 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s