ErrorException

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

Introducción

Una excepción para los errores.

Sinopsis de la Clase

class ErrorException extends Exception {
/* Propiedades */
protected int $severity = E_ERROR;
/* Propiedades heredadas */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* Métodos */
public __construct(
    string $message = "",
    int $code = 0,
    int $severity = E_ERROR,
    ?string $filename = null,
    ?int $line = null,
    ?Throwable $previous = null
)
final public getSeverity(): int
/* Métodos heredados */
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}

Propiedades

severity

La severidad de la excepción

Ejemplos

Ejemplo #1 Uso de set_error_handler() para cambiar todos los mensajes de error en ErrorException

<?php

set_error_handler
(function (int $errno, string $errstr, string $errfile, int $errline) {
if (!(
error_reporting() & $errno)) {
// Este código de error no está incluido en error_reporting.
return;
}

if (
$errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
// No lanzar excepción para advertencias de obsolescencia, ya que nuevas o inesperadas
// obsolescencias podrían romper la aplicación.
return;
}

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

// Deserializar datos corruptos desencadena una advertencia que será convertida en
// ErrorException por el manejador de errores.
unserialize('broken data');

?>

El resultado del ejemplo sería algo similar a:

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

Tabla de contenidos