6 Best Methods for Exception Handling in ASP.NET MVC

An exception is a problem that arises during the execution of a program. Exceptions help you deal with any unexpected or exceptional situations that occur when a program is running by a way to transfer control from one part of the program to another. There are 6 ways of handling Exceptions in ASP.NET MVC.

Method 1: Simple way

Method 2: Override “OnException” method

Method 3: Using “HandleError” Attribute

Method 4: Inheriting from “HandleErrorAttribute”

Method 5: Handling HTTP errors

Method 6: Global Error handling in MVC

The exception classes in C# are mainly directly or indirectly derived from the System.Exception class. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.

Method 1: Simple way

The simplest way is to use the traditional .NET exception handling style (i.e. try catch finally block). Now when exception happens, catch block gets executed and it redirects to the error view, as shown in the method SomeError() below.

However if we use this method, we will not be utilizing MVC exception mechanism properly and completely. In the next sections, we will discuss five important ways by which we can utilize MVC provided features for more effective exception handling.

Method 2: Override “OnException” Method

In this method, we can override the “OnException” event of the controller and set the “Result” to the view name. The view gets invoked when error occurs in the controller. In the code below, you can see we have set the “Result” to a view named as “Error”.

We have also set the exception so that it can be displayed inside the view.

We can use the code below to display the above error in view.

The problem with this approach is that we cannot reuse the error handling logic across multiple controllers.

Method 3: Using “HandleError” Attribute

The other way of handling error is by using “HandleError” attribute. Implementing “HandleError” attribute is a two-step process:

Step 1: We need to first decorate the action method with “HandleError” attribute, as shown in the code below.

Step 2: In the “Web.config” file, you need to add the “customErrors” tag and point to the “Error” view as shown in the “Web.config” code snippet below.

In the event you want different error views for different exception types, you can decorate action method with multiple “HandleError” attribute point to multiple views, as per exception types.

Method 4: Inheriting from “HandleErrorAttribute”

One of the biggest drawbacks of all the previous methods was reusability. Error handling logic cannot be reused across other controllers.

In order to reuse error handling logic across controllers, we can inherit from “HandleErrorAttribute”class and decorate this class as attribute across controller.

Method 5: Handling HTTP Errors

All MVC exception handling techniques discussed up to this point do not handle HTTP errors like file not found, HTTP 500 errors, and so on. For that, we need to make an entry of the error action and the error status code, as shown in the config file below.

Method 6: Global Error handling in MVC

If you wish to do global error handling across your application, you can override the “Application_Error” event and do a response.redirect from the global error event. So if the error handling is not done at the controller level, it will get propagated to “Global.asax” file.

What’s the best practice?

The best practice is a combination of Method 4 and Method 6, that is, to  create error handling classes which inherit from “HandleErrorAttribute” class and decorate them respectively on controllers and action methods. This takes care of errors happening on controllers and actions.

As a precaution, enable Global error handling as a fallback for any unexpected and unhandled errors by using “Application_Error” event, as described in “Method 6”.

Leave a Reply

Your email address will not be published.