What is HttpException Class in C#?
Ask by ICSM Computer
Updated 05 May 2025
The
HttpExceptionclass in ASP.NET represents errors that occur during the processing of HTTP requests. It is part of theSystem.Webnamespace and is used to handle web-related runtime errors such as:Namespace
Definition
HttpExceptioninherits fromExternalException→SystemException→Exception.Common Constructors
Common Properties
MessageErrorCodeWebEventCodeGetHttpCode()GetHtmlErrorMessage()HRESULTof the error.Example: Throwing a 404 Not Found
This throws a 404 error and can trigger a custom error page if configured.
Example: Throwing a 500 Internal Server Error
Using in Custom Error Handling
In
Global.asax.cs:When to Use
HttpExceptionSummary Table
throw new HttpException(404, "Not Found")throw new HttpException(500, "Server Error", ex)httpEx.GetHttpCode()Application_Error