---
title: "What is HttpException Class in C#?"  
description: "What is HttpException Class in C#?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34078/what-is-httpexception-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 6 minutes  

---

# What is HttpException Class in C#?

The `HttpException` class in ASP.NET represents errors that occur **during the processing of HTTP requests**. It is part of the `System.Web` namespace and is used to handle **web-related runtime errors** such as:

1. Missing files (404)
2. Unauthorized access (401)
3. Server errors (500)
4. Custom application errors

## Namespace

```cs
using System.Web;
```

## Definition

```cs
public class HttpException : System.Runtime.InteropServices.ExternalException
```

`HttpException` inherits from `ExternalException` → `SystemException` → `Exception`.

## Common Constructors

```cs
new HttpException()
new HttpException(string message)
new HttpException(string message, Exception innerException)
new HttpException(int httpCode, string message)
new HttpException(int httpCode, string message, Exception innerException)
```

## Common Properties

| Property | Description |
| --- | --- |
| `Message` | A description of the error |
| `ErrorCode` | The numeric error code (from ExternalException) |
| `WebEventCode` | ASP.NET health monitoring code |
| `GetHttpCode()` | Returns the HTTP status code (e.g., 404, 500) |
| `GetHtmlErrorMessage()` | Returns an HTML-formatted error string |
| [Data](https://learn.microsoft.com/en-us/dotnet/api/system.exception.data?view=netframework-4.8.1#system-exception-data) | Gets a collection of key/value pairs that provide additional user-defined information about the exception. |
| [ErrorCode](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.externalexception.errorcode?view=netframework-4.8.1#system-runtime-interopservices-externalexception-errorcode) | Gets the `HRESULT` of the error. |
| [HelpLink](https://learn.microsoft.com/en-us/dotnet/api/system.exception.helplink?view=netframework-4.8.1#system-exception-helplink) | Gets or sets a link to the help file associated with this exception. |
| [HResult](https://learn.microsoft.com/en-us/dotnet/api/system.exception.hresult?view=netframework-4.8.1#system-exception-hresult) | Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. |
| [InnerException](https://learn.microsoft.com/en-us/dotnet/api/system.exception.innerexception?view=netframework-4.8.1#system-exception-innerexception) | Gets the [Exception](https://learn.microsoft.com/en-us/dotnet/api/system.exception?view=netframework-4.8.1) instance that caused the current exception. |
| [Message](https://learn.microsoft.com/en-us/dotnet/api/system.exception.message?view=netframework-4.8.1#system-exception-message) | Gets a message that describes the current exception. |
| [Source](https://learn.microsoft.com/en-us/dotnet/api/system.exception.source?view=netframework-4.8.1#system-exception-source) | Gets or sets the name of the application or the object that causes the error. |
| [StackTrace](https://learn.microsoft.com/en-us/dotnet/api/system.exception.stacktrace?view=netframework-4.8.1#system-exception-stacktrace) | Gets a string representation of the immediate frames on the call stack. |
| [TargetSite](https://learn.microsoft.com/en-us/dotnet/api/system.exception.targetsite?view=netframework-4.8.1#system-exception-targetsite) | Gets the method that throws the current exception. |
| [WebEventCode](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpexception.webeventcode?view=netframework-4.8.1#system-web-httpexception-webeventcode) | Gets the event codes that are associated with the HTTP exception. |

## Example: Throwing a 404 Not Found

```cs
throw new HttpException(404, "Page Not Found");
```

This throws a 404 error and can trigger a custom error page if configured.

## Example: Throwing a 500 Internal Server Error

```cs
try
{
    // Some code that fails
}
catch (Exception ex)
{
    throw new HttpException(500, "Internal server error", ex);
}
```

## Using in Custom Error Handling

In `Global.asax.cs`:

```cs
protected void Application_Error()
{
    Exception ex = Server.GetLastError();
    HttpException httpEx = ex as HttpException;

    int httpCode = httpEx != null ? httpEx.GetHttpCode() : 500;

    if (httpCode == 404)
    {
        Response.Redirect("~/Error/NotFound");
    }
    else
    {
        Response.Redirect("~/Error/ServerError");
    }
}
```

## When to Use `HttpException`

1. To **signal HTTP-specific errors** in your app logic.
2. To **wrap lower-level exceptions** in web-friendly messages.
3. When integrating with **ASP.NET error handling or custom error pages**.
4. To **log and categorize** errors by HTTP code (404, 403, etc.).

## Summary Table

| Feature | Example |
| --- | --- |
| Throw 404 | `throw new HttpException(404, "Not Found")` |
| Throw with inner exception | `throw new HttpException(500, "Server Error", ex)` |
| Get status code | `httpEx.GetHttpCode()` |
| Use in `Application_Error` | For centralized error redirection |

## Answers

### Answer by ICSM Computer

The `HttpException` class in ASP.NET represents errors that occur **during the processing of HTTP requests**. It is part of the `System.Web` namespace and is used to handle **web-related runtime errors** such as:

1. Missing files (404)
2. Unauthorized access (401)
3. Server errors (500)
4. Custom application errors

## Namespace

```cs
using System.Web;
```

## Definition

```cs
public class HttpException : System.Runtime.InteropServices.ExternalException
```

`HttpException` inherits from `ExternalException` → `SystemException` → `Exception`.

## Common Constructors

```cs
new HttpException()
new HttpException(string message)
new HttpException(string message, Exception innerException)
new HttpException(int httpCode, string message)
new HttpException(int httpCode, string message, Exception innerException)
```

## Common Properties

| Property | Description |
| --- | --- |
| `Message` | A description of the error |
| `ErrorCode` | The numeric error code (from ExternalException) |
| `WebEventCode` | ASP.NET health monitoring code |
| `GetHttpCode()` | Returns the HTTP status code (e.g., 404, 500) |
| `GetHtmlErrorMessage()` | Returns an HTML-formatted error string |
| [Data](https://learn.microsoft.com/en-us/dotnet/api/system.exception.data?view=netframework-4.8.1#system-exception-data) | Gets a collection of key/value pairs that provide additional user-defined information about the exception. |
| [ErrorCode](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.externalexception.errorcode?view=netframework-4.8.1#system-runtime-interopservices-externalexception-errorcode) | Gets the `HRESULT` of the error. |
| [HelpLink](https://learn.microsoft.com/en-us/dotnet/api/system.exception.helplink?view=netframework-4.8.1#system-exception-helplink) | Gets or sets a link to the help file associated with this exception. |
| [HResult](https://learn.microsoft.com/en-us/dotnet/api/system.exception.hresult?view=netframework-4.8.1#system-exception-hresult) | Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. |
| [InnerException](https://learn.microsoft.com/en-us/dotnet/api/system.exception.innerexception?view=netframework-4.8.1#system-exception-innerexception) | Gets the [Exception](https://learn.microsoft.com/en-us/dotnet/api/system.exception?view=netframework-4.8.1) instance that caused the current exception. |
| [Message](https://learn.microsoft.com/en-us/dotnet/api/system.exception.message?view=netframework-4.8.1#system-exception-message) | Gets a message that describes the current exception. |
| [Source](https://learn.microsoft.com/en-us/dotnet/api/system.exception.source?view=netframework-4.8.1#system-exception-source) | Gets or sets the name of the application or the object that causes the error. |
| [StackTrace](https://learn.microsoft.com/en-us/dotnet/api/system.exception.stacktrace?view=netframework-4.8.1#system-exception-stacktrace) | Gets a string representation of the immediate frames on the call stack. |
| [TargetSite](https://learn.microsoft.com/en-us/dotnet/api/system.exception.targetsite?view=netframework-4.8.1#system-exception-targetsite) | Gets the method that throws the current exception. |
| [WebEventCode](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpexception.webeventcode?view=netframework-4.8.1#system-web-httpexception-webeventcode) | Gets the event codes that are associated with the HTTP exception. |

## Example: Throwing a 404 Not Found

```cs
throw new HttpException(404, "Page Not Found");
```

This throws a 404 error and can trigger a custom error page if configured.

## Example: Throwing a 500 Internal Server Error

```cs
try
{
    // Some code that fails
}
catch (Exception ex)
{
    throw new HttpException(500, "Internal server error", ex);
}
```

## Using in Custom Error Handling

In `Global.asax.cs`:

```cs
protected void Application_Error()
{
    Exception ex = Server.GetLastError();
    HttpException httpEx = ex as HttpException;

    int httpCode = httpEx != null ? httpEx.GetHttpCode() : 500;

    if (httpCode == 404)
    {
        Response.Redirect("~/Error/NotFound");
    }
    else
    {
        Response.Redirect("~/Error/ServerError");
    }
}
```

## When to Use `HttpException`

1. To **signal HTTP-specific errors** in your app logic.
2. To **wrap lower-level exceptions** in web-friendly messages.
3. When integrating with **ASP.NET error handling or custom error pages**.
4. To **log and categorize** errors by HTTP code (404, 403, etc.).

## Summary Table

| Feature | Example |
| --- | --- |
| Throw 404 | `throw new HttpException(404, "Not Found")` |
| Throw with inner exception | `throw new HttpException(500, "Server Error", ex)` |
| Get status code | `httpEx.GetHttpCode()` |
| Use in `Application_Error` | For centralized error redirection |


---

Original Source: https://www.mindstick.com/interview/34078/what-is-httpexception-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
