ErrorException

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

Introduction

Une exception pour les erreurs.

Synopsis de la classe

class ErrorException extends Exception {
/* Propriétés */
protected int $severity = E_ERROR;
/* Propriétés héritées */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* Méthodes */
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éthodes héritées */
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}

Propriétés

severity

La sévérité de l'exception

Exemples

Exemple #1 Utilisation de set_error_handler() pour changer tous les messages d'erreurs en ErrorException

<?php

set_error_handler
(function (int $errno, string $errstr, string $errfile, int $errline) {
if (!(
error_reporting() & $errno)) {
// Ce code d'erreur n'est pas inclus dans error_reporting.
return;
}

if (
$errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
// Ne pas lancer d'exception pour les avertissements d'obsolescence, car de nouvelles ou inattendues
// obsolescences pourraient casser l'application.
return;
}

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

// Désérialiser des données corrompues déclenche un avertissement qui sera converti en
// ErrorException par le gestionnaire d'erreurs.
unserialize('broken data');

?>

Résultat de l'exemple ci-dessus est similaire à :

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

Sommaire