Parola chiave cattura PHP
Esempio
Cattura un'eccezione:
<?php
try {
throw new Exception("This is an exception");
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
Definizione e utilizzo
La catch
parola chiave viene utilizzata per gestire le eccezioni generate dal codice in un blocco try precedente .
Pagine correlate
La throw
parola chiave.
La try
parola chiave.
La finally
parola chiave.
Leggi di più su try..catch.finally (Eccezioni) nel nostro Tutorial sulle eccezioni PHP .
Altri esempi
Esempio
Usa catch per più tipi di eccezione:
<?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";
}
?>
❮ Parole chiave PHP