Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / PHP

Erreurs et exceptions

Throwable couvre Exception et Error, dont TypeError.

Version : PHP 8.4; repository runtime, production runtime not attested

Syntaxe

catch (Throwable $failure) { /* handle */ }

Throwable couvre Exception et Error, dont TypeError. Lire une clé absente produit un avertissement plutôt qu’une exception. Un gestionnaire peut convertir un avertissement choisi en ErrorException. Rétablissez ensuite le gestionnaire précédent.

set_error_handler(function($level,$message){throw new ErrorException($message,0,$level);},E_WARNING);
try {$row=[]; $x=$row["absent"];} catch(Throwable $e){echo get_class($e)," ",$e->getSeverity()===E_WARNING?"warning":"other";} finally {restore_error_handler();}
ErrorException warning

Exemples

<?php
set_error_handler(function($level,$message){throw new ErrorException($message,0,$level);},E_WARNING);
try {$row=[]; $x=$row["absent"];} catch(Throwable $e){echo get_class($e)," ",$e->getSeverity()===E_WARNING?"warning":"other";} finally {restore_error_handler();}

Résultat

ErrorException warning

Sources