ErrorException

- « Exception::__clone

- ErrorException::__construct »

- PHP Manual
- Predefined Exceptions
- ErrorException

# ErrorException

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

## Introduction

An Error Exception.

## Class synopsis

class ErrorException extends Exception {
/* Properties */
protected int $severity = E_ERROR;
/* Inherited properties */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* Methods */
public __construct(
string `$message` = "",
int `$code` = 0,
int `$severity` = `E_ERROR`,
?string `$filename` = `null`,
?int `$line` = `null`,
?Throwable `$previous` = `null`
)
final public getSeverity(): int
/* Inherited methods */
final public Exception::getMessage(): string
final public Exception::getPrevious(): ?Throwable
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
final public Exception::getTraceAsString(): string
public Exception::__toString(): string
private Exception::__clone(): void
}

## Properties

severity

  The severity of the exception

## Examples

Example #1 Use set_error_handler() to change error messages into ErrorException

    <?php

    set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
        if (!(error_reporting() & $errno)) {
            // This error code is not included in error_reporting.
            return;
        }

        if ($errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
            // Do not throw an Exception for deprecation warnings as new or unexpected
            // deprecations would break the application.
            return;
        }

        throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
    });

    // Unserializing broken data triggers a warning which will be turned into an
    // ErrorException by the error handler.
    unserialize('broken data');

    ?>

The above example will output something similar to:

    Fatal error: Uncaught ErrorException: unserialize(): Error at offset 0 of 11 bytes in test.php:16
    Stack trace:
    #0 [internal function]: {closure}(2, 'unserialize(): ...', 'test.php', 16)
    #1 test.php(16): unserialize('broken data')
    #2 {main}
      thrown in test.php on line 16

## Table of Contents

- ErrorException::__construct — Constructs the exception
- ErrorException::getSeverity — Gets the exception severity