ErrorException

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

Introdução

Uma exceção Error.

Resumo da classe

class ErrorException extends Exception {
/* Propriedades */
protected int $severity = E_ERROR;
/* Propriedades herdadas */
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 herdados */
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}

Propriedades

severity

O nível da exceção

Exemplos

Exemplo #1 Use a função set_error_handler() para transformar mensagens de erro em ErrorException.

<?php
set_error_handler
(function (int $errno, string $errstr, string $errfile, int $errline) {
if (!(
error_reporting() & $errno)) {
// Este código de erro não é incluso em error_reporting.
return;
}
if (
$errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
// Não lance um Exception para avisos de obsolescência porque avisos
// inesperados podem quebrar a aplicação.
return;
}
throw new
\ErrorException($errstr, 0, $errno, $errfile, $errline);
});

// Desserializar dados errados emite um aviso que será convertido em um
// ErrorException pelo manipulador.
unserialize('broken data');
?>

O exemplo acima produzirá algo semelhante 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

Índice