In this article, we will be discussing various ways of handling an exception in ASP.NET MVC.

Below are the different ways -

Now, let’s elaborate one by one.

Try-Catch

This is the default way of handling exceptions where we write our source code into the try block and catch the exceptions in the catch block. However, you can have multiple catch blocks for a try block. Even you can have Try-Catch blocks inside a Try block.

Below is the example for the same.

  1. public IActionResult Index()
  2. {
  3. try
  4. {
  5. int i = 10;
  6. i = i / 0;
  7. return View();
  8. }
  9. catch (Exception Ex)
  10. {
  11. return View("Error");
  12. }
  13. }

In the above example, we are handling an exception by using Try-Catch. In Try block, we have written the code that needs to be executed, however, if an error occurs, the catch bock handles the error and redirects it to View.

Note

In a Catch block, you can log the error either in a file or in a database.

Override onException from Controller class

You can override OnException(ExceptionContext filterContext), which belongs to Controller class. And you can pass this to the base Exception, as shown below.

  1. public class HomeController : Controller
  2. {
  3. protected overridevoid OnException(ExceptionContext filterContext)
  4. {
  5. base.OnException(filterContext);
  6. }
  7. public ActionResult Index()
  8. {
  9. return View();
  10. }
  11. }

In the above sample, we are overriding onException() and passing the exception to a base method.

Now, you will be redirected to the default exception page. To overcome this, i.e., to show custom view, use the below example.

  1. protected override void OnException(ExceptionContext filterContext)
  2. {
  3. ViewResult view = new ViewResult();
  4. view.ViewName = "Error";
  5. filterContext.Result = view;
  6. filterContext.ExceptionHandled = true;
  7. }

In the above example, we are creating a view result and passing the view name to it. And after that, we are passing that view to filtercontext.

Note

In this case, we must intimate the filtercontext that we have handled the error. For this, we need to write the below line of code.

filterContext.ExceptionHandled = true;

Now, in the above scenarios, we will have to handle the exception in every controller. To overcome this, we create a BaseController class which will implement the Controller class and our all controllers will implement Base Contoller class. See below.

Add BaseController Class which implements a Controller class.

  1. public class BaseController: Controller
  2. {
  3. protected override void OnException(ExceptionContext filterContext)
  4. {
  5. //base.OnException(filterContext);
  6. ViewResult view = new ViewResult();
  7. view.ViewName = "Error";
  8. filterContext.Result = view;
  9. filterContext.ExceptionHandled = true;
  10. }
  11. }

Now, implement BaseController to every Controller like HomeController.

  1. public class HomeController : BaseController
  2. {
  3. public ActionResult Index()
  4. {
  5. int i = 10;
  6. i = i / 0;
  7. return View();
  8. }
  9. }

HandleError Attribute

ASP.NET MVC HandleError attribute provides a built-in exception filter. It can be applied on a specific action method or at controller. However, we can add it at the global level for handling exception in controller and action level. While creating our application, it is automatically included within the Global.asax.cs and registered in FilterConfig.cs, as shown below.

  1. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  2. {
  3. filters.Add(new HandleErrorAttribute());
  4. }

Now, we need to set the mode to on status for customErrors node of system.web section of web.config file.

  1. <customErrors mode="On"></customErrors>

Now, we can use HanldeError attribute at any of the action methods like Index.

  1. public class HomeController : Controller
  2. {
  3. [HandleError()]
  4. public ActionResult Index()
  5. {
  6. int i = 10;
  7. i = i / 0;
  8. return View();
  9. }
  10. }

Even you can add it at controller level so that it will be applicable to all the action methods present in the controller.

  1. [HandleError()]
  2. public class HomeController : Controller
  3. {
  4. public ActionResult Index()
  5. {
  6. int i = 10;
  7. i = i / 0;
  8. return View();
  9. }
  10. public ActionResult About()
  11. {
  12. ViewBag.Message = "Your application description page.";
  13. return View();
  14. }
  15. }

HandleError attribute has some properties as explained below.

Below are some of the examples.

  1. public class HomeController : Controller
  2. {
  3. [HandleError(View = "NullError", ExceptionType = typeof(NullReferenceException))]
  4. [HandleError(Order = 2, View = "Error", ExceptionType = typeof(DivideByZeroException))]
  5. public ActionResult Index()
  6. {
  7. int i = 10;
  8. i = i / 0;
  9. return View();
  10. }
  11. }

CustomeException by HandleErrorAttribute

Now, we can achieve custom exception handling by implementing the HandleError attribute which has on ExceptionContext exceptionContext. Below is the detailed description of the same.

[CustomErrorHandling]
public ActionResult GetRoles()

Below is the implementation for the same.

  1. public override void OnException(ExceptionContext exceptionContext)
  2. {
  3. if (!exceptionContext.ExceptionHandled)
  4. {
  5. string controllerName = (string)exceptionContext.RouteData.Values["controller"];
  6. string actionName = (string)exceptionContext.RouteData.Values["action"];
  7. Exception custException = new Exception("There is some error");
  8. Log.Error(exceptionContext.Exception.Message+ " in " + controllerName);
  9. var model = new HandleErrorInfo(custException, controllerName, actionName);
  10. exceptionContext.Result = new ViewResult
  11. {
  12. ViewName = "~/Views/Shared/Error.cshtml",
  13. ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
  14. TempData = exceptionContext.Controller.TempData
  15. };
  16. exceptionContext.ExceptionHandled = true;
  17. }
  18. }

Below is the sample of Error.cshtml.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width" />
  5. <title>Error</title>
  6. </head>
  7. <body>
  8. <hgroup>
  9. <h1>Error.</h1>
  10. @model System.Web.Mvc.HandleErrorInfo
  11. @if (Model != null)
  12. {
  13. <div>
  14. <h2>
  15. @Model.Exception.Message in @Model.ControllerName
  16. </h2>
  17. </div>
  18. }
  19. </hgroup>
  20. </body>
  21. </html>