PHP中常见的错误与异常处理总结大全

当我们开发程序时,程序出现问题是很常见的,当出现了异常与错误我们该如何处理呢?本文将详细给大家介绍PHP错误与异常处理的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:

一、PHP错误处理

1.语法错误

2.运行时错误

3.逻辑错误:不提示错误,但功能不对,最麻烦

4.三种级别:notice/warning/fatal error(无法继续执行)

5.错误报告显示:

a.可以在php.ini中修改error_reporting项目,以限定错误报告类型,如:error_reporting=E_ALL & ~E_NOTICE

b.只修改某个脚本内的错误显示,可以使用error_reporting(E_ALL & ~E_NOTICE); (推荐)

6.自定义错误报告:set_error_handler()可以传入用以显示错误的预定参数,如下:

set_error_handler('reportError'); $mess=""; function reportError($error_type,$error_message,$error_file,$error_line){ global $mess; $mess.="发生错误级别为{$error_type}类型,错位信息<b>{$error_message}</b>,在文件{$error_file}中,第{$error_line}行。<br>"; } getType($a); echo "1111111<br>"; getType(); echo "2222<br>"; echo $mess; /*发生错误级别为8类型,错位信息Undefined variable: a,在文件F:\projects\Frame\FrameTest\BackEnd\regularExpression.php中,第24行。 发生错误级别为2类型,错位信息gettype() expects exactly 1 parameter, 0 given,在文件F:\projects\Frame\FrameTest\BackEnd\regularExpression.php中,第26行。*/

7.记录错误日志

a.将PHP.ini中display_errors设置为Off,log_errors设置为On

b.自定义日志目录error_log="C:/XX/XX/php_error.log"

PHP中常见的错误与异常处理总结大全


c.也可以使用ini_set("display_errors","Off")或ini_get在脚本内部进行设定

二、PHP异常处理

1.try catch一体的,中间不能有任何代码

2.Exception是系统预定义的类

3.如果有异常对象抛出,就将异常对象给catch中的类

4.try中发生异常位置后的代码不再继续执行,而是直接转到catch中执行

try{ echo "开车上班<br>"; throw new Exception("车子爆胎了!"); }catch(Exception $e){//相当于Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo '换上备胎,继续上班<br>'; }

5.异常处理可以配合错误处理一起使用

set_error_handler('reportError'); function reportError($error_type,$error_message,$error_file,$error_line){ if($error_type==E_WARNING){ throw new Exception("错误信息:{$error_message},错误文件:{$error_file},错误行数{$error_line}"); } } function drive($a){ echo $a; } try{ echo "开车上班<br>"; drive();//忘记传参,触发自定义错误函数中警告性错误,抛出异常 }catch(Exception $e){//相当于Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo "换上备胎,继续上班<br>"; }

6.自定义异常类

a.Exception类是所有异常的基类,没有定义具体异常的处理方法(只有些获取提示的方法)

b.自定义的异常类必须是系统类的子类

c.如果继续了Exception类,重写了构造方法,不要忘记调用父类构造方法进行初始化

class BTException extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "打开后备箱,拿出工具,换备胎"; } } try{ echo "开车上班<br>"; throw new BTException("车子爆胎了!"); }catch(BTException $e){//相当于Exception $e = new Exception(''); echo $e->getMessage().'<br>'; echo $e->method().'<br>'; echo "换上备胎,继续上班<br>"; }

7.捕获多个异常,注:try中还可嵌套try

class Err1 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "纠正错误1"; } } class Err2 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "纠正错误2"; } } class Err3 extends Exception { function __construct($message){ parent::__construct($message); } function method(){ return "纠正错误3"; } } $rand=rand(1,3); try{ switch($rand){ case 1: throw new Err1("发生错误1"); case 2: throw new Err2("发生错误2"); case 3: throw new Err3("发生错误3"); } }catch(Err1 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }catch(Err2 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }catch(Err3 $e){ echo $e->getMessage().'<br>'; echo $e->method().'<br>'; }

总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2c8f70752008eb49d574a41d5a767a66.html