PHP catch 关键字
示例
捕获异常
<?php
try {
throw new Exception("这是一个异常");
} catch(Exception $e) {
echo $e->getMessage();
}
?>
自己动手试一试 »
定义和用法
catch
关键字用于处理前面 try 块中抛出的异常。
相关页面
throw
关键字。
The try
keyword.
The finally
keyword.
在我们的 PHP 异常教程 中阅读更多关于 try..catch.finally(异常)的内容。
更多示例
示例
使用 catch 处理多种类型的异常
<?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 "捕获了一个越界异常";
} catch(LogicException $e) {
echo "捕获了一个逻辑异常";
} catch(Exception $e) {
echo "捕获了一个普通异常";
}
?>
自己动手试一试 »
❮ PHP 关键字