2011-04-19

ASP.Net HTTP Status 404

I've seen 2 common ways to throw HTTP 404 errors in ASP.Net (I'm using C# for the code examples)...

throw new System.Web.HttpException(404, "{Custom not found message here}");

And...

Response.Clear();
Response.StatusCode = 404;
Response.End();


While it would seem like the first one would be the best all-around option, it has a large problem when implemented in the middle of a try/catch block... It gets caught. By using the Response object instead, you can let the browser handle the status code by whatever means it would normally display such a response from the server.