Exception handling in ASP.NET MVC applications

For exception handling we have been using try..catch..finally block since forever in various languages. In this blog we will see how to handle the unhandled exceptions in ASP.NET MVC. Unhandled exceptions are the exceptions that are not handled using try..catch..finally block. For handling unhandled exceptions or for creating a global exception handler we have to perform following steps:

1. In web.config add following line in the system.web section

<customErrors mode=”RemoteOnly”></customErrors>

2. Before each controller add following annotation:


public ActionResult Index()

3. In Views folder create a sub-folder named Shared and create a file there named Error. Extension can be .cshtml (razor) or .aspx (web forms engine)

4. Optionally to the Error file add following code to display the actual error message:



In case if you want to name the Error page something else then change the annotation before controller to [HandleError(View=”custom_name”)]. Replace custom_name with whatever your error page name is.

  1. No comments yet.
(will not be published)