PHP - Catch -Schlüsselwort
Beispiel
Ausnahme abfangen:
<?php
try {
throw new Exception("This is an exception");
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
Definition und Verwendung
Das Schlüsselwort wird verwendet, um Ausnahmen zu behandeln, die vom Code in einem vorangehenden try-Blockcatch
ausgelöst werden .
Verwandte Seiten
Das throw
Schlüsselwort.
Das try
Schlüsselwort.
Das finally
Schlüsselwort.
Lesen Sie mehr über try..catch.finally (Exceptions) in unserem PHP Exceptions Tutorial .
Mehr Beispiele
Beispiel
Verwenden Sie catch für mehrere Arten von Ausnahmen:
<?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";
}
?>
❮ PHP-Schlüsselwörter