title: Globally handling exceptions in Web API 2
body: <p>I'm trying to learn and understand global exception handling in Web API 2. When I step through the following code, I thought I'd hit my break point in Handle method but I don't. </p>
  
  <p>What am I missing?</p>
  
  <p>Here's what I've done: I created a new Web API project in Visual Studio 2013 Update 4. In my root, I created the following class -- named GlobalExceptionHandler.cs that looks like this:</p>
  
  <pre><code>using System.Net.Http;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Web.Http;
  using System.Web.Http.ExceptionHandling;
  
  public class GlobalExceptionHandler : ExceptionHandler
  {
     public override void Handle(ExceptionHandlerContext context)
     {
  
        // --&gt; Break Point in the next line &lt;--
        string str1 = context.Exception.Message;
     } 
  }
  </code></pre>
  
  <p>This is what my Startup.cs looks like:</p>
  
  <pre><code>using Owin;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Http;
  using System.Web.Http.ExceptionHandling;
  
  public class Startup
  {
     public void Configuration(IAppBuilder app)
     {
       var config = new HttpConfiguration();
  
       config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
  
       config.MapHttpAttributeRoutes();
  
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
  
        app.UseWebApi(config);
     }
  }
  </code></pre>
  
  <p>When I try to generate an exception in my web api method, I was expecting to hit the Handle method in GlobalExceptionHandler but I'm not.</p>
  
  <pre><code>public IHttpActionResult Get()
  {
     throw new HttpResponseException(HttpStatusCode.BadRequest);
     return Ok("I should not get here because of exception");
  }
  </code></pre>