标签:code cas bre echo this span end throw extends
目的:
使用多个catch捕获不同的异常
eg:
<?php //创建三个Exception class AException extends Exception{ function printMsg(){ echo "This is AException."; } } class BException extends Exception{ function printMsg(){ echo "This is BException."; } }class CException extends Exception{ function printMsg(){ echo "This is CException."; } } //一个try,多个catch捕获不同的异常 try{ $flag = 1; switch ($flag) { case 1: throw new AException(‘AException:‘); break; case 2: throw new BException(‘BException:‘); break; case 3: throw new CException(‘CException:‘); break; default: break; } } catch(AException $e){ echo $e->getmessage(); $e->printMsg(); } catch(BException $e){ echo $e->getmessage(); $e->printMsg(); } catch(CException $e){ echo $e->getmessage(); $e->printMsg(); } catch(Exception $e){ //这个catch主要是捕获漏网之鱼 echo $e->getmessage(); }
输出:
AException:This is AException.
标签:code cas bre echo this span end throw extends
原文地址:https://www.cnblogs.com/lyc94620/p/12862794.html