<?php //创建可抛出一个异常的函数 function checkNum($number) { if($number>1) { throw new Exception("Value must be 1 or below"); } return true; } checkNum(2); ?>
Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in G:phpStudyPubliczititest.php:7 Stack trace: #0 G:phpStudyWWWmircowebmircowebwwwrootPubliczititest.php(12): checkNum(2) #1 {main} thrown in G:phpStudyWWWmircowebmircowebwwwrootPubliczititest.php on line 7
<?php //创建可抛出一个异常的函数 function checkNum($number) { if($number>1) { throw new Exception("Value must be 111 or below"); } return true; } //在 "try" 代码块中触发异常 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(); } ?>
Message: Value must be 111 or below