In this article, we will be discussing various ways of handling an exception in ASP.NET MVC.
Below are the different ways -
- Try-Catch
- Override onException from Controller class
- HandleError Attribute
- CustomException by HandleErrorAttribute
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.
- try
A Try block is used to encapsulate a code. If any code throws an exception within that Try block, the exception will be handled by the corresponding Catch. - catch
When an exception occurs, the Catch block of code gets it. This is the place where we are able to handle the exceptions.
Below is the example for the same.
- public IActionResult Index()
- {
- try
- {
- int i = 10;
- i = i / 0;
- return View();
- }
- catch (Exception Ex)
- {
- return View("Error");
- }
- }
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.
- public class HomeController : Controller
- {
- protected overridevoid OnException(ExceptionContext filterContext)
- {
- base.OnException(filterContext);
- }
- public ActionResult Index()
- {
- return View();
- }
- }
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.
- protected override void OnException(ExceptionContext filterContext)
- {
- ViewResult view = new ViewResult();
- view.ViewName = "Error";
- filterContext.Result = view;
- filterContext.ExceptionHandled = true;
- }
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.
- public class BaseController: Controller
- {
- protected override void OnException(ExceptionContext filterContext)
- {
- //base.OnException(filterContext);
- ViewResult view = new ViewResult();
- view.ViewName = "Error";
- filterContext.Result = view;
- filterContext.ExceptionHandled = true;
- }
- }
Now, implement BaseController to every Controller like HomeController.
- public class HomeController : BaseController
- {
- public ActionResult Index()
- {
- int i = 10;
- i = i / 0;
- return View();
- }
- }
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.
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- }

margam chakriPosted Mar 17, 2021, 6:24 PM
My question is if we use multiple error handlings in same page which is the order of error methods.
MhKh MhLPosted May 8, 2019, 3:50 AM
Thanks !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Imran JalaliPosted Oct 2, 2018, 7:55 AM
Nice clear Explanations.