---
title: "How to Achive Error Handling in ASP.NET MVC"  
description: "How to Achive Error Handling in ASP.NET MVC"  
author: "Anonymous User"  
published: 2014-11-21  
updated: 2014-11-22  
canonical: https://www.mindstick.com/forum/12674/how-to-achive-error-handling-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["exception handling"]  
reading_time: 2 minutes  

---

# How to Achive Error Handling in ASP.NET MVC

How can I correctly [handle exceptions](https://www.mindstick.com/forum/159234/how-do-you-handle-exceptions-using-the-try-catch-block-in-java) thrown from [controllers](https://www.mindstick.com/forum/155773/how-asynchronous-controllers-work-in-asp-dot-net-mvc) in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc)? The HandleErrorattribute seems to only process exceptions thrown by the MVC [infrastructure](https://www.mindstick.com/news/2313/a-cybersecurity-and-infrastructure-security-agency-will-monitor-the-us-midterm-elections) and not exceptions thrown by my own code.

**Using this [web.config](https://www.mindstick.com/forum/183/web-config-file)**

```
<customErrors mode="On">    <error statusCode="401" redirect="/Errors/Http401" /></customErrors>with the following codenamespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index()        {            // Force a 401 exception for testing            throw new HttpException(401, "Unauthorized");        }    }}
```

doesn't result in what I was hoping for. Instead I get the generic ASP.NET error page [telling me](https://answers.mindstick.com/qa/50024/my-computer-is-telling-me-my-startup-drive-is-nearly-full-how-to-i-make-room) to modify my web.config to see the actual error information. However, if instead of throwing an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) I return an invalid View, I get the /Shared/Views/Error.aspx page:

return View("DoesNotExist");

Throwing exceptions within a [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) like I've done above seems to bypass all of the HandleErrorfunctionality, so what's the right way to create [error pages](https://answers.mindstick.com/qa/95430/how-can-i-remove-error-pages-and-empty-pages-from-my-website-so-that-they-should-not-be-visible-on-google-webmaster-being-an-seo) and how do I play nice with the MVC infrastructure?

## Replies

### Reply by Anonymous User

Thanks to kazimanzurrashaid, here is what I wound up doing in Global.asax.cs:

```
protected void Application_Error(){    Exception unhandledException = Server.GetLastError();    HttpException httpException = unhandledException as HttpException;    if (httpException == null)    {        Exception innerException = unhandledException.InnerException;        httpException = innerException as HttpException;    }    if (httpException != null)    {        int httpCode = httpException.GetHttpCode();        switch (httpCode)        {            case (int) HttpStatusCode.Unauthorized:                Response.Redirect("/Http/Error401");                break;        }    }}
```

I'll be able to add more pages to the HttpContoller based on any additional HTTP error codes I need to support.


---

Original Source: https://www.mindstick.com/forum/12674/how-to-achive-error-handling-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
