任何程序员在开发时都可能遇到过一些失误,或其他原因造成错误的发生。当然,用户如果不愿意或不遵循应用程序的约束,也会在使用时引起一些错误发生。下面这篇文章主要给大家介绍了关于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"
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>';
}
总结
以上就是php中关于错误与异常处理的详解的详细内容。