这篇文章主要为大家详细介绍了php封装一个异常的处理类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了php自定义异常处理类,供大家参考,具体内容如下
一、代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>自定义异常处理类</title>
</head>
<body>
<?php
class TelException extends Exception{
public function errorTel(){
$errorMsg = "出错原因:" . $this ->getMessage(). "不是一个合法的电话号码" ;
$errorMsg .= "<br>" ;
$errorMsg .= "错误文件路径:" . $this ->getFile();
$errorMsg .= "<br>" ;
$errorMsg .= "错误代码行号:" . $this -> getLine();
return $errorMsg ;
}
}
function check_tel( $tel ){
$checkphone = "/^13(\\d{9})$/" ;
$counts =preg_match( $checkphone , $tel );
return $counts ;
}
$tel = "133891gfj" ;
try {
if (check_tel( $tel ) !=1){
throw new TelException( $tel );
}
} catch (TelException $e ){
include_once ( "error.php" );
}
?>
</body>
</html>
</body>
</html>
|
Copy after login
二、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
The above is the detailed content of PHP encapsulates an exception handling class. For more information, please follow other related articles on the PHP Chinese website!