# ErrorHandler

> ErrorHandler handles uncaught PHP errors and exceptions.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › ErrorHandler

| abstract class | `yii\base\ErrorHandler` |
| --- | --- |
| Extends | [`Component`](/docs/api/yii/base/component/index.md) » [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/base/ErrorHandler.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/ErrorHandler.php#L28) |

2 subclasses:

- [`yii\console\ErrorHandler`](/docs/api/yii/console/errorhandler/index.md)
- [`yii\web\ErrorHandler`](/docs/api/yii/web/errorhandler/index.md)

ErrorHandler handles uncaught PHP errors and exceptions.

ErrorHandler is configured as an application component in [`Application`](/docs/api/yii/base/application/index.md) by default.
You can access that instance via `Yii::$app->errorHandler`.

For more details and usage information on ErrorHandler, see the [guide article on handling errors](/docs/guide/runtime-handling-errors/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`$discardExistingOutput`](#property-discardExistingOutput) | `bool` | Whether to discard any existing page output before error display. Default: `true`. | `ErrorHandler` |
| <span id="property-exception"></span>`$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php)`\|null` | The exception that is being handled currently. Default: `null`. | `ErrorHandler` |
| [`$memoryReserveSize`](#property-memoryReserveSize) | `int` | The size of the reserved memory. Default: `262144`. | `ErrorHandler` |
| <span id="property-silentExitOnException"></span>`$silentExitOnException` | `bool` | If true - `handleException()` will finish script with `ExitCode::OK`. false - `ExitCode::UNSPECIFIED_ERROR`. Default: `null`. Available since 2.0.36. | `ErrorHandler` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`clearOutput()`](#method-clearOutput) | Removes all output echoed before calling this method. | `ErrorHandler` |
| [`convertExceptionToString()`](#method-convertExceptionToString) static | Converts an exception into a simple string. | `ErrorHandler` |
| [`convertExceptionToVerboseString()`](#method-convertExceptionToVerboseString) static | Converts an exception into a string that has verbose information about the exception and its trace. | `ErrorHandler` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`handleError()`](#method-handleError) | Handles PHP execution errors such as warnings and notices. | `ErrorHandler` |
| [`handleException()`](#method-handleException) | Handles uncaught PHP exceptions. | `ErrorHandler` |
| [`handleFatalError()`](#method-handleFatalError) | Handles fatal PHP errors. | `ErrorHandler` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) |  | `ErrorHandler` |
| [`logException()`](#method-logException) | Logs the given exception. | `ErrorHandler` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`register()`](#method-register) | Register this error handler. | `ErrorHandler` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`unregister()`](#method-unregister) | Unregisters this error handler by restoring the PHP error and exception handlers. | `ErrorHandler` |
| [`handleFallbackExceptionMessage()`](#method-handleFallbackExceptionMessage) protected | Handles exception thrown during exception processing in [`handleException()`](/docs/api/yii/base/errorhandler/index.md#method-handleException). | `ErrorHandler` |
| [`renderException()`](#method-renderException) protected abstract | Renders the exception. | `ErrorHandler` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_SHUTDOWN"></span>`EVENT_SHUTDOWN` | [`Event`](/docs/api/yii/base/event/index.md) | An event that is triggered when the handler is called by shutdown function via [`handleFatalError()`](/docs/api/yii/base/errorhandler/index.md#method-handleFatalError). Event name: `'shutdown'`. Available since 2.0.46. | `ErrorHandler` |

## Property details

### $discardExistingOutput <span id="property-discardExistingOutput"></span>

```php
public bool $discardExistingOutput = true
```

Whether to discard any existing page output before error display.

Defaults to true.

### $memoryReserveSize <span id="property-memoryReserveSize"></span>

```php
public int $memoryReserveSize = 262144
```

The size of the reserved memory.

A portion of memory is pre-allocated so that
when an out-of-memory issue occurs, the error handler is able to handle the error with
the help of this reserved memory. If you set this value to be 0, no memory will be reserved.
Defaults to 256KB.

## Method details

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

```php
public function clearOutput(): mixed
```

Removes all output echoed before calling this method.

**Returns**

`mixed`

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

```php
public static function convertExceptionToString(Throwable $exception): string
```

Converts an exception into a simple string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception being converted |

**Returns**

`string`: The string representation of the exception.

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

```php
public static function convertExceptionToVerboseString(Throwable $exception): string
```

Converts an exception into a string that has verbose information about the exception and its trace.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception being converted |

**Returns**

`string`: The string representation of the exception.

**Since**

2.0.14

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

```php
public function handleError(int $code, string $message, string $file, int $line): bool
```

Handles PHP execution errors such as warnings and notices.

This method is used as a PHP error handler. It will simply raise an [`ErrorException`](/docs/api/yii/base/errorexception/index.md).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$code` | `int` | The level of the error raised. |
| `$message` | `string` | The error message. |
| `$file` | `string` | The filename that the error was raised in. |
| `$line` | `int` | The line number the error was raised at. |

**Returns**

`bool`: Whether the normal error handler continues.

**Throws**

- [`ErrorException`](/docs/api/yii/base/errorexception/index.md)

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

```php
public function handleException(Throwable $exception): mixed
```

Handles uncaught PHP exceptions.

This method is implemented as a PHP exception handler.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception that is not caught |

**Returns**

`mixed`

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

```php
public function handleFatalError(): mixed
```

Handles fatal PHP errors.

**Returns**

`mixed`

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

```php
public function init(): mixed
```

**Returns**

`mixed`

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

```php
public function logException(Throwable $exception): mixed
```

Logs the given exception.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception to be logged |

**Returns**

`mixed`

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

```php
public function register(): mixed
```

Register this error handler.

**Returns**

`mixed`

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

```php
public function unregister(): mixed
```

Unregisters this error handler by restoring the PHP error and exception handlers.

**Returns**

`mixed`

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

```php
protected function handleFallbackExceptionMessage(Throwable $exception, Throwable $previousException): mixed
```

Handles exception thrown during exception processing in [`handleException()`](/docs/api/yii/base/errorhandler/index.md#method-handleException).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | Exception that was thrown during main exception processing. |
| `$previousException` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | Main exception processed in [`handleException()`](/docs/api/yii/base/errorhandler/index.md#method-handleException). |

**Returns**

`mixed`

**Since**

2.0.11

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

```php
abstract protected function renderException(Throwable $exception): mixed
```

Renders the exception.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$exception` | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception to be rendered. |

**Returns**

`mixed`