title: Why ASP.NET MVC sometimes returns error status code 500 and sometimes error status code 404
body: <p>I am trying to implement custom error pages in ASP.NET MVC 5. If I input a invalid URL, the custom error page will show my custom 404 not found page most of the times with the correct response "404 not found", but in some cases it responses with a my custom 500 internal server error page with the response "500 internal server error"??</p>
  
  <p>For example, I have the following routing configuration:</p>
  
  <pre><code>routes.MapRoute(
      null,
      "Category/{category}",
      new { controller = "Post", action = "Category" }
  );
  </code></pre>
  
  <p>Now if I try to navigate to the following pages which doesn't exists, it will respond 404 not found error, which is correct.</p>
  
  <p>justatestsite.com/Category/noexists (follows the correct routing configuration, page doesn't exists, correct error status 404)</p>
  
  <p>justatestsite.com/Category/noexists/norouteconfig (doesn't follow the correct routing configuration, page doesn't exists, correct error status 404)</p>
  
  <p>However with my other routing configuration:</p>
  
  <pre><code>routes.MapRoute(
      null,
      "Archive/{year}/{month}/{day}/{title}",
      new { controller = "Post", action = "Detail" }
  );
  </code></pre>
  
  <p>justatestsite.com/archive/2014/8/25/noexists (follows the correct routing configuation, page doesn't exists, incorrect error status 500)</p>
  
  <p>justatestsite.com/archive/2014/8/25/noexists/norouteconfig (doesn't follow the correct routing configuration, page doesn't exists, correct error status 404)</p>
  
  <p>Here is my post detail controller</p>
  
  <pre><code>public ViewResult Detail(int year, int month, int day, string title)
  {
      var post = repository.Detail(year, month, day, title);
  
      if(post == null)
      {
          throw new HttpException(404, "Post not found");
      }
  
      if (post.Published == false &amp;&amp; User.Identity.IsAuthenticated == false)
      {
          throw new HttpException(401, "The post is not published");
      }
  
      return View(post);
  }
  </code></pre>
  
  <p>Why is it sometimes responding with error status code 500?????</p>