Capturer le mot-clé PHP
Exemple
Intercepter une exception :
<?php
try {
throw new Exception("This is an exception");
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
Définition et utilisation
Le catch
mot-clé est utilisé pour gérer les exceptions levées par le code dans un bloc try précédent .
Pages connexes
Le throw
mot clé.
Le try
mot clé.
Le finally
mot clé.
En savoir plus sur try..catch.finally (Exceptions) dans notre tutoriel PHP Exceptions .
Plus d'exemples
Exemple
Utilisez catch pour plusieurs types d'exception :
<?php
try {
$rand = rand(0, 2);
switch($rand) {
case 0: throw new Exception();
case 1: throw new
OutOfBoundsException();
case 2: throw new
LogicException();
}
} catch(OutOfBoundsException $e) {
echo "Caught an out of bounds exception";
} catch(LogicException $e) {
echo "Caught a logic exception";
} catch(Exception $e) {
echo
"Caught an ordinary exception";
}
?>
❮ Mots clés PHP