In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
小编给大家分享一下PHP如何自定义异常类,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
/* * 1.自定义的异常类,必须是系统类Exception的子类 * 如果继承Exception类,重写了构造方法,一定要调用一下父类的构造方法。 */ class MyException extends Exception{//必须继承Exception类 function __construct($mess){ parent::__construct($mess); } function change(){//自定义方法 echo '换上备胎'; } }echo "早上起床"; $flag = true;//根据异常情况设置标志位来抛出异常 try{ echo "开车上班"; if($flag){ //抛出异常 throw new MyException("车子爆胎"); } echo "路况很好";}catch(MyException $e){//$e = new Exception(); echo $e->getMessage().""; echo $e->change()."";//调用自定义类的方法 echo "换上备胎,继续开车上班";}echo "到公司";============================================================================/* * 捕获多个异常catch */ class WcException extends Exception{ function methodWc(){ echo "去公厕"; } } class NoeatException extends Exception{ function methodEat(){ echo "买个面包"; } } class NutException extends Exception{ function methodGowork(){ echo "换个轮胎"; } } class SnowException extends Exception{ function methodHighway(){ echo "走小路"; } } class Badday{ function gowc($flag){ if(!$flag){ throw new WcException("马桶坏了"); } echo "物质准备到位"; } function eat($time){ if(!$time){ throw new NoeatException("无早餐可吃"); } echo "吃很饱"; } function gowork($nut){ if(!$nut){ throw new NutException("扎到钉子了"); } echo "去上班啦!"; } function highway($snow){ if(!$snow){ throw new SnowException("下雪封路了"); } echo "走高速"; } }echo "早上起床"; $flag = false;//根据异常情况设置标志位来抛出异常 try{ $badday = new Badday(); //1.上厕所(马桶不好用了) 去公厕 $badday->gowc(true); //2.吃早餐(没有早点了) 买个面包 $badday->eat(false); //3.开车上班(爆胎) 换胎 $badday->gowork(true); //4.上高速(下雪了) 走国道 $badday->highway(true);}catch(WcException $e){ echo $e->methodWc();}catch (NoeatException $e){ echo $e->methodEat();}catch(NutException $e){ echo $e->methodGowork();}catch(SnowException $e){ echo $e->methodHighway();}catch (Exception $e){ echo $e->getMessage();}echo "到公司";以上是"PHP如何自定义异常类"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.