Exception or Error Handling and Logging in MVC4

Exception or Error Handling and Logging in MVC4

01 Apr 2024
Intermediate
40K Views
8 min read

Exception or Error Handling: An Overview

Error handling is the main concern in any application, whether it is a web application or a desktop application. Usually, we catch the exception and log its details to a database or text, XML file and also display a user-friendly message to the end-user in place of the error. In this MVC Tutorial, we will explore more about theException or Error Handling which will include the HandleError filter in mvc, customize error handling in mvc4, how to handle and log errors in mvc, and log exceptions in mvc. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

Asp.Net MVC has some built-in exception filters. HandleError is the default built-in exception filter. Let's see how to use this filter within your application.

HandleError Attribute

The HandleErrorAttribute filter works only when custom errors are enabled in the Web.config file of your application. You can enable custom errors by adding a customErrors attribute inside the <system.web> node, as shown below:


</system.web>
...
<customErrors mode="On" defaultRedirect="Error.htm"/>
</system.web>
  

HandleError Attribute can be used to handle errors at Action Method level, Controller level and Global level.

HandleError Attribute at Action Method Level

Simply put this attribute to the action method tells MVC how to react when an exception occurs.


[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public ActionResult Index(int id)
{
 var db = new MyDataContext();
 return View("Index", db.Categories.Single(x => x.Id == id));
}

In the above example when a database exception (System.Data.DataException) occurs during the execution of the Index action, MVC will display the DatabaseError view.

HandleError Attribute at Controller Level

Simply put this attribute to the controller tells MVC how to react when an exception occurs within the action methods of this controller.

[HandleError(ExceptionType = typeof(System.Data.DataException), View = "DatabaseError")]
public class HomeController : Controller
{
 /* Controller Actions with HandleError applied to them */
}

In the above example when a database exception (System.Data.DataException) occurs during the execution of the controller's any action method, MVC will display the DatabaseError view.

HandleError Attribute at Global Level

You can also apply the HandleError Attribute for the entire application by registering it as a global error handler. For registering a global error handler, open the FilterConfig.cs file within the App_Start folder to find the RegisterGlobalFilters method.

By default, the ASP.NET MVC template has already registered a global HandleErrorAttribute to the GlobalFilterCollection for your application. Here you can also add your own custom filter to the global filter collection as well.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new HandleErrorAttribute
 {
 ExceptionType = typeof(System.Data.DataException),
 View = "DatabaseError"
 });

 filters.Add(new HandleErrorAttribute()); //by default added
}

Note

Always remember, by default, global filters are executed in their registered order. so register error filters for specific exception types before any other.

You can also set the order of execution of these filters by giving a number of values to each. The above-registered filters can be re-written as:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new HandleErrorAttribute(),2); //by default added
 filters.Add(new HandleErrorAttribute
 {
 ExceptionType = typeof(System.Data.DataException),
 View = "DatabaseError"
 },1);
} 

Now, all the filters will be executed in the number sequence.

Limitation of HandleError Attribute

  1. It has no support to log the exceptions since it suppresses the error once it is handled.

  2. It only catches 500 HTTP errors and doesn't catch other HTTP errors like 404,401 etc.

  3. It doesn't catch the errors that occur outside the controllers like errors that occur in the model.

  4. It returns an error view even if an error occurred in AJAX calls that is not useful on the client side. It would be great to return a piece of JSON in AJAX exceptions.

Extending HandleError Filter for logging and handling more errors

You can also make your custom error filter by extending HandleErrorthe filter. Let's see how to extend this filter to log the exceptions and return a JSON object for AJAX calls.

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
 public override void OnException(ExceptionContext filterContext)
 {
 if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
 {
 return;
 }
 
 if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
 {
 return;
 }
 
 if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
 {
 return;
 }
 
 // if the request is AJAX return JSON else view.
 if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
 {
 filterContext.Result = new JsonResult 
 { 
 JsonRequestBehavior = JsonRequestBehavior.AllowGet, 
 Data = new
 { 
 error = true,
 message = filterContext.Exception.Message
 } 
 };
 }
 else
 {
 var controllerName = (string)filterContext.RouteData.Values["controller"];
 var actionName = (string)filterContext.RouteData.Values["action"];
 var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
 
 filterContext.Result = new ViewResult
 {
 ViewName = View,
 MasterName = Master,
 ViewData = new ViewDataDictionary(model),
 TempData = filterContext.Controller.TempData
 };
 }
 
 // log the error by using your own method
 LogError(filterContext.Exception.Message, filterContext.Exception);
 
 filterContext.ExceptionHandled = true;
 filterContext.HttpContext.Response.Clear();
 filterContext.HttpContext.Response.StatusCode = 500;
 
 filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
 }
}
Conclusion

So in this article, we have learned about the Exception or Error Handling and Logging in MVC4. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification

FAQs

Q1. What is logging and exception handling?

Logging and exception handling are like two peas in a pod. When a problem happens in your Java code, that typically means you have an exception that needs to be handled, and of course, any time an error or unanticipated event occurs, that occurrence should be logged appropriately

Q2. What is error handling vs error logging?

Logging is your lens into what's happening during execution, providing insights for both development and operations teams.

Q3. What is the exception handling?

Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this