PHP finally 关键字
实例
不管是否抛出异常都运行一些代码:
<?php
echo "Starting the process.";
try {
// Select randomly between 0 and 1, throw an exception if 1 is selected.
$random = rand(0, 1);
if($random == 1) {
throw new Exception("Exception");
}
} finally {
echo "Process complete";
}
?>
亲自试一试 »
echo "Starting the process.";
try {
// Select randomly between 0 and 1, throw an exception if 1 is selected.
$random = rand(0, 1);
if($random == 1) {
throw new Exception("Exception");
}
} finally {
echo "Process complete";
}
?>
定义和用法
finally
关键字用于 try...finally
和 try...catch...finally
结构来运行代码块,无论是否发生异常。
相关页面
throw
关键字。
catch
关键字。
finally
关键字。
在我们的 PHP 异常教程中了解更多关于异常的信息。