# HttpException

> HttpException represents an exception caused by an improper request of the end-user.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › HttpException

| class | `yii\web\HttpException` |
| --- | --- |
| Extends | [`yii\base\UserException`](/docs/api/yii/base/userexception/index.md) » [`yii\base\Exception`](/docs/api/yii/base/exception/index.md) » [`Exception`](https://www.php.net/manual/en/class.exception.php) |
| Since | 2.0 |
| Source | [`framework/web/HttpException.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/HttpException.php#L31) |

13 subclasses:

- [`BadRequestHttpException`](/docs/api/yii/web/badrequesthttpexception/index.md)
- [`ConflictHttpException`](/docs/api/yii/web/conflicthttpexception/index.md)
- [`ForbiddenHttpException`](/docs/api/yii/web/forbiddenhttpexception/index.md)
- [`GoneHttpException`](/docs/api/yii/web/gonehttpexception/index.md)
- [`MethodNotAllowedHttpException`](/docs/api/yii/web/methodnotallowedhttpexception/index.md)
- [`NotAcceptableHttpException`](/docs/api/yii/web/notacceptablehttpexception/index.md)
- [`NotFoundHttpException`](/docs/api/yii/web/notfoundhttpexception/index.md)
- [`RangeNotSatisfiableHttpException`](/docs/api/yii/web/rangenotsatisfiablehttpexception/index.md)
- [`ServerErrorHttpException`](/docs/api/yii/web/servererrorhttpexception/index.md)
- [`TooManyRequestsHttpException`](/docs/api/yii/web/toomanyrequestshttpexception/index.md)
- [`UnauthorizedHttpException`](/docs/api/yii/web/unauthorizedhttpexception/index.md)
- [`UnprocessableEntityHttpException`](/docs/api/yii/web/unprocessableentityhttpexception/index.md)
- [`UnsupportedMediaTypeHttpException`](/docs/api/yii/web/unsupportedmediatypehttpexception/index.md)

HttpException represents an exception caused by an improper request of the end-user.

HttpException can be differentiated via its [`$statusCode`](/docs/api/yii/web/httpexception/index.md#property-statusCode) property value which
keeps a standard HTTP status code (e.g. 404, 500). Error handlers may use this status code
to decide how to format the error page.

Throwing an HttpException like in the following example will result in the 404 page to be displayed.

```
if ($item === null) { // item does not exist
    throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
}
```

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-statusCode"></span>`$statusCode` | `int` | HTTP status code, such as 403, 404, 500, etc. Default: `null`. | `HttpException` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `HttpException` |
| [`getName()`](#method-getName) |  | `HttpException` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(int $status, string|null $message = null, int $code = 0, Throwable|null $previous = null)
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$status` | `int` | HTTP status code, such as 404, 500, etc. |
| `$message` | `string\|null` | Error message |
| `$code` | `int` | Error code |
| `$previous` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php)`\|null` | The previous exception used for the exception chaining. |

### getName() <span id="method-getName"></span>

```php
public function getName(): string
```

**Returns**

`string`: The user-friendly name of this exception