PHP catch Mot-clé

❮ Mots-clés PHP

Exemple

Attraper une exception :

<?php
try {
throw new Exception("Ceci est une exception");
} catch(Exception $e) {
echo $e->getMessage();
}
?>
Essayez-le vous-même »

Définition et Utilisation

Le catch est un mot-clé utilisé pour gérer les exceptions qui sont lancées par le code dans un bloc try précédent.


Pages Associées

Le throw est un mot-clé.

Le try est un mot-clé.

Le finally est un mot-clé.

Lisez-en plus sur try..catch.finally (Exceptions) dans notre Tutoriel sur les Exceptions PHP .


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 "Exception hors limites attrapée";
} catch(LogicException $e) {
echo "Exception logique attrapée";
} catch(Exception $e) {
echo "Exception ordinaire attrapée";
}
?>
Essayez-le vous-même »

❮ Mots-clés PHP