title: HandleErrorAttribute derived class is not catching exceptions?
body: <p>I have a ASP.NET MVC web app and I am trying to improve its error handling capabilities.  I read up on the HandleErrorAttribute base class and wanted to use that with my controller classes.  However, when I throw a test Exception from a method in one of my controller classes that is decorated with that attribute, the <strong>OnException</strong> override in my derived class is <em>not</em> triggered.  Instead of my derived error handling class's OnException method being triggered, I see a generic error message in XML format and the generic error message is <em>not found anywhere in my app</em>:</p>
  
  <pre><code>&lt;Error&gt;
      &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>What am I doing wrong?  </p>
  
  <p>Note I tried adding the following custom errors element to the <em>system.web</em> element in my <strong>web.config</strong> file as indicated by this SO post, but <em>it didn't help</em>:</p>
  
  <p><a href="https://stackoverflow.com/questions/619582/asp-net-mvc-handleerror-not-catching-exceptions">ASP.net MVC [HandleError] not catching exceptions</a></p>
  
  <pre><code>&lt;customErrors mode="On" defaultRedirect="Error" /&gt;
  </code></pre>
  
  <p>Here is my HandleErrorAttribute derived class:</p>
  
  <pre><code>public class HandleControllerErrors : HandleErrorAttribute
  {
      public override void OnException(ExceptionContext filterContext)
      {
          // Breakpoint set here never hit.
  
          Exception ex = filterContext.Exception;
          filterContext.ExceptionHandled = true;
          var model = new HandleErrorInfo(filterContext.Exception, "Controller", "Action");
  
          filterContext.Result = new ViewResult()
          {
              // Use the ErrorInController view.
              ViewName = "ErrorInController",
              ViewData = new ViewDataDictionary(model)
          };
      }
  } 
  </code></pre>
  
  <p>Here is how I decorate my controller class.  I have a method in it that forcibly throws an exception that is <em>not</em> inside a <em>try/catch</em> block.:</p>
  
  <pre><code>[HandleControllerErrors]
  public class ValuesController : ApiController
  {
      [Route("Api/TestException/")]
      public IHttpActionResult TestException()
      {
          throw new InvalidCastException("Fake invalid cast exception.");
      }
  }
  </code></pre>