Palavra- chave de captura do PHP
Exemplo
Pegue uma exceção:
<?php
try {
throw new Exception("This is an exception");
}
catch(Exception $e) {
echo $e->getMessage();
}
?>
Definição e uso
A catch
palavra-chave é usada para lidar com exceções lançadas pelo código em um bloco try anterior .
Páginas relacionadas
A palavra- throw
chave.
A palavra- try
chave.
A palavra- finally
chave.
Leia mais sobre try..catch.finally (Exceções) em nosso Tutorial de Exceções do PHP .
Mais exemplos
Exemplo
Use catch para vários tipos de exceção:
<?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";
}
?>
❮ Palavras-chave PHP