首页 > Web开发 > 详细

php抛出异常

时间:2020-07-18 16:22:25      阅读:44      评论:0      收藏:0      [点我收藏+]
<?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:\phpStudy\Public\zititest.php:7 Stack trace: #0 G:\phpStudy\WWW\mircoweb\mircoweb\wwwroot\Public\zititest.php(12): checkNum(2) #1 {main} thrown in G:\phpStudy\WWW\mircoweb\mircoweb\wwwroot\Public\zititest.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

 

php抛出异常

原文:https://www.cnblogs.com/newmiracle/p/13334950.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!