<?php //創(chuàng)建可拋出一個(gè)異常的函數(shù) function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//在 "try" 代碼塊中觸發(fā)異常 try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//捕獲異常 catch(**Exception $e**)
{
echo 'Message: ' .$e->getMessage();
}
?>
php是弱類型語(yǔ)言,catch中為什么要寫Exception $e ,而不是直接寫$e?
叮當(dāng)貓咪
2023-05-02 17:13:28