【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-17_7.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
//自定义一个异常处理类并继承Exception类
class useException extends Exception
{
//重新实现父类的方法
public function __construct($message, $code = 0) {
//确保所有变量都被正确赋值
parent::__construct($message, $code);
}
//重新定义异常信息的显示样式
public function __toString() {
return __CLASS__ . ":[{$this->code}]:{$this->message}";
}
//自定义显示样式
public function showError() {
return "<b>[{$this->code}]</b>:<font color='red'> {$this->message}</font><br>";
}
}
//定义一个函数,用于测试自定义的异常处理类
function testException($value){
switch($value){
case 1:
throw new useException("异常错误1");
break;
case 2:
throw new useException("异常错误2",2);
break;
case 3:
throw new Exception("异常错误3");
break;
default:
echo "正常运行";
}
}
//测试函数
try{
testException(1);
}catch(useException $e){
print $e->showError();
}
try{
testException(2);
}catch(useException $e){
print $e->showError();
}
try{
testException(3);
}catch(useException $e){
print $e->showError();
}catch(Exception $e){
print $e->getMessage();
}
?>