In web.config:
<customErrors mode="RemoteOnly" defaultRedirect="~/Errors.aspx">
<error statusCode="404" redirect="~/Error.aspx?code=404"/>
</customErrors>
in page’s code behind:
throw new HttpException(404, "404 Not Found");
On this line, visual studio breaks with an error:
System.Web.HttpException was unhandled
by user code
Where should I catch this error?
If the Visual Studio IDE is breaking on the throw line, then you are running under debug mode. In debug mode, exceptions cause execution to stop.
Try running the website (CTRL+F5) instead of debugging the website (F5). However, note that you will then get the regular yellow ASP.NET exception page, as your customErrors tag specifices “RemoteOnly”, which means the custom page will only be displayed to remote requests. Your request is not remote, as you are on the machine serving the page.
To see the customErrors in action, change the tag to:
and run your website using CTRL+F5.