所以一个函数有时会崩溃(未捕获的异常),我想在延迟 1 秒的情况下重新调用它 2 次。这是代码,但它似乎不起作用:
$crashesn = 0;
function checkNum($number) {
echo $number . "<br>";
if ($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
try {
checkNum(2);
echo 'If you see this, the number is 1 or below';
}
catch(Exception $e) {
$crashesn++;
echo "crashes number:".$crashesn."<br>";
if ($crashesn <= 2) {
sleep(1);
checkNum(2);
} else {
echo "MESSAGE: " .$e->getMessage();
}
}
checknum 是抛出异常的函数(这里它每次都通过抛出异常而崩溃)。问题是,当我运行这段代码时,我的页面上仍然出现错误消息
Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in G:\fix\ta_dll\test.php:30 Stack trace: #0 c:\wwwl\test.php(45): checkNum(2) #1 {main} thrown in c:\php\test.php on line 30
而不是“消息:错误描述”。“崩溃编号”行只打印一次,而不是两次。
有谁知道我做错了什么?