title: How to configure multiple exception handlers
body: <p>I am trying to configure my middleware pipeline to use 2 different exception handlers to handle the same exception.  For example, I'm trying to have both my custom handler and in-built DeveloperExceptionPageMiddleware as follows:</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();                
          app.ConfigureCustomExceptionHandler();
      }
      else
      {
          app.UseExceptionHandler("/Home/Error");                               
          app.ConfigureCustomExceptionHandler();            
          app.UseHsts();
      }
  
      app.UseHttpsRedirection();
      app.UseStaticFiles();
      app.UseCookiePolicy();
      app.UseAuthentication();
      app.UseMvcWithDefaultRoute();
  }
  </code></pre>
  
  <p>My objective is to have the custom handler do its own thing (logging, telemetry, etc), and then pass on (next()) to the other in-built handler which displays a page.  My custom handler looks like this:</p>
  
  <pre><code>public static class ExceptionMiddlewareExtensions
  {
      public static void ConfigureCustomExceptionHandler(this IApplicationBuilder app)
      {            
          app.UseExceptionHandler(appError =&gt;
          {
              appError.Use(async (context, next) =&gt;
              {                    
                  var contextFeature = context.Features.Get&lt;IExceptionHandlerFeature&gt;();
                  if (contextFeature != null)
                  {
                      //log error / do custom stuff
  
                      await next();
                  }
              });
          });
      }
  }
  </code></pre>
  
  <p>I cannot get CustomExceptionHandler to pass on processing to the next middleware.  I get the following page instead:</p>
  
  <p>404 error:</p>
  
  <p><a href="https://i.stack.imgur.com/vqQmS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vqQmS.png" alt="enter image description here"></a></p>
  
  <p>I tried switching around the order, but then the developer exception page takes over and the custom exception handler is not called.</p>
  
  <p>Is what I'm trying to do possible at all?</p>
  
  <p><strong>Update:</strong></p>
  
  <blockquote>
    <p>The solution was to take Simonare's original suggestion and re-throw the exception in the <code>Invoke</code> method.  I also had to remove any type of response-meddling by replacing the following in <code>HandleExceptionAsync</code> method:</p>
    
    <p><code>context.Response.ContentType = "application/json";
    context.Response.StatusCode = (int)code;
    return context.Response.WriteAsync(result);</code></p>
    
    <p>with:</p>
    
    <p><code>return Task.CompletedTask;</code></p>
  </blockquote>