---
title: "Exception Handling in ASP.NET MVC3"  
description: "In this article, we will learn about the HandleError filter and discuss about the exception handling mechanisms that will fit to an MVC3 application."  
author: "Vijay Shukla"  
published: 2012-10-27  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1052/exception-handling-in-asp-dot-net-mvc3  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 3 minutes  

---

# Exception Handling in ASP.NET MVC3

##### Introduction:-

In this article, we will learn about the [HandleError](https://www.mindstick.com/forum/2325/how-do-mvc-handleerror) filter and discuss about the exception [handling mechanisms](https://www.mindstick.com/forum/160175/describe-rust-s-error-handling-mechanisms-including-the-result-and-option-types) that will fit to an MVC3 [application](https://www.mindstick.com/articles/12824/calculator-application-in-android). [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is a serious issue in any application. Whether it's web or desktop. Implementing an appropriate exception handling is compulsory in every application. ASP.NET MVC3 comes with some built-in support for exception handling through exception filters. The HandleError attribute is the default built-in exception filter.

##### [HandleError] Attribute:-

[HandleError] [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) that can be applied over an action or a controller or even at a [global level](https://answers.mindstick.com/qa/100947/what-are-the-challenges-in-addressing-cybersecurity-threats-at-a-global-level). When you create a MVC3 application you will see the HandleErrorAttribute is added to the GlobalFiltersCollection in the Global.asax.cs which under the Global.asax.

```
public static void RegisterGlobalFilters(GlobalFilterCollection filters){      filters.Add(new HandleErrorAttribute());}
```

##### \
Working of HandleError Attribute:-

The HandleError attribute handles the exceptions that are raised by the controller

\

actions, filters and views, it will return a custom view named Error.cshtml which is

\

placed in the Shared folder. The HandleError filter works only if

\

the <customErrors>section is turned On in web.config under the <system.web> tag.

```
<system.web>                …………………………                <customErrors mode="On"/></system.web>
```

\

Note:- HandleError Filter handles the exception while customErrors turn On in Web.config file by default customErrors isReadonly

##### Now write that code in HomeController.

```
    public ActionResult Index()        {            ViewBag.Message = "Welcome to ASP.NET MVC!";            return View();        }
        [HandleError(ExceptionType = typeof(DivideByZeroException), Master = "_Layout", View     = "Error")]
               public ActionResult About()        {            string str;            object obj = null;            str = obj.ToString();            return View();
        }
```

##### [HandleError] attribute have four properties:-

\

1. ExceptionType:- The ExceptionType allows you to specify what

\

exception that attribute should handle

\

2. Master: - The Master allows you to manage which [master page](https://www.mindstick.com/forum/649/set-header-and-footer-with-in-master-page) (or as

\

Razor refers to them, Layout) you desire to render.

\

3. Order: -

\

4. View: - The View allows you to specify which error view (page) you want

\

it to redirect to.

\

```
[HandleError(ExceptionType = typeof(DivideByZeroException), Master = "_Layout", Order = 1, View = "Error")]
```

This code will Generate the runtime exception “Object reference not set to an

\

[instance of an object](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object)” when execute the About () action. [HandleError] attribute will

\

handle the error and call the Error.cshtml View which is under the Shared

\

Folder or Home Folder (created by user View).

![Exception Handling in ASP.NET MVC3](https://www.mindstick.com/mindstickarticle/3b3db6b5-a464-4ca6-9d77-8b89e824054c/images/01d6f0d9-285c-492c-ab4d-c4fce3af774f.png)

##### In Error.cshtml:-

```
@model System.Web.Mvc.HandleErrorInfo@{    ViewBag.Title = "Error";
} <h2>Error</h2><h2>    Sorry, an error occurred as processing your request.</h2>
```

Now Run That Project and select the About Menu:-

![Exception Handling in ASP.NET MVC3](https://www.mindstick.com/mindstickarticle/3b3db6b5-a464-4ca6-9d77-8b89e824054c/images/d25e18f3-0b95-4227-80f6-09f3b3b33f82.png)

After Run and select the About Menu you will see on URL bar call the “Home/About” but here is render the Error.cshtml View that the reason is raises [Runtime Exceptions](https://www.mindstick.com/forum/159442/handle-runtime-exceptions-in-mern).

---

Original Source: https://www.mindstick.com/articles/1052/exception-handling-in-asp-dot-net-mvc3

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
