Run some code regardless of whether an exception was thrown:
<?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";
}
?>
Try it Yourself »
The finally
keyword is used in try...finally
and try...catch...finally
structures to run a block of code whether or not an exception occurred.
The throw
keyword.
The catch
keyword.
The finally
keyword.
Read more about exceptions in our PHP Exceptions Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!