php 错误处理

1,常见的错误和异常
a,拼写错误
b,单引号和双引号的混用
c,括号使用混乱
d,等号与赋值符号混淆
e,缺少美元符号
f,调用不存在的常量和变量
g,调用不存在的文件
h,环境配置错误
j,数据库服务器连接错误


2,错误处理

php.ini---错误处理

3,应用die语句调试
使用die语句调试,不仅可以显示错误的位置,还可以输出错误信息。一旦出现错误,程序将会终止运行,并在浏览器上显示出错之前的信息和错误信息
<?php
if (!file_exists ("welcome.txt")){  //判断文件是否存在
die ("文件不存在");
}else{
$file=fopen("welcome.txt","r");
}
?>

4,自定义错误和错误触发器

自定义的错误函数的语法格式:

error_function(error_level(错误规定错误报告级别,必须是整数),error_message(为用户定义的错误规定错误消息),error_file(错误发生的文件名),error_line(行),error_ontext(规定一个数组,但错误发生时在用的每个变量及它们的值))

eg:

function customError ($errno,$errstr){
echo "<b>错误</b>[$errno]$errstr<br/>";
echo "终止程序";
die();
}

规定在何时触发:
set _error_handler(error_function,error_types)
error_functiion为必须参数,规定发生错误时运行的函数,

针对所有的错误来使用自定义错误处理程序:
set——error_handler("customError");

eg:
<?php
//定义错误函数
function customError($errno,$errstr){
echo "<b>错误:</b>[$errno]$errstr";
}
//设置错误函数的处理
set_error_handler("customError");
//触发自定义错误函数
echo ($test);
?>

trigger_error(error_message(规定错误消息,1024个字符以内),error_types(规定错误类型))
eg:
<?php
$test =5;
if($test>4){
trigger_error ("Value must be 4 or below");  //创建自定义错误信息
}
?>
eg:
<?php
//定义错误函数
function customError($errno,$errstr){
echo "<b>错误:</b>[$errno]$errstr";
}
//设置错误函数的处理
set_error_handler("customError",$_USER_WARNING);
//trigger_error函数
$test=5;
if($test>4){
trigger_error("Value must be 4 or below",E_USER_WARNING);
}
?>

//错误记录
通过电子邮件向用户自己发送错误消息是一种获得指定错误通知的好办法。
<?php
//定义错误函数
function customError ($errno, $errstr){
echo "<b>错误:</b>[$errno]$errstr<br/>";
echo "错误记录已经发送完毕";
error_log("错误:[$errno]$errstr",1,"someone@example.com","From:webmastere@example.com");
}
//设置错误函数的处理
set_error_handler("customError",E_USER_WARNING);
//trigger_error函数
$test =5;
if ($test>4){
trigger_error("Value must be 4 or below",E_USER_WARNING);
}
?>

//异常处理

1,异常的基本处理方法
eg:抛出一个异常,同时不去捕获它。
<?php
//创建带有异常的函数
function checkNum($number){
if($number>1){
throw new Exception("Value must be 1 or below");
}
return true;
     }
//抛出异常
checkNum(2);
?>
//避免上免得错误
eg:
<?php
//创建可抛出一个异常的函数
function checkNum ($number){
if ($number>1){     //创建函数,检测数字是否大于1,是则抛出一个异常
throw new Exception ("数值必须小于或等于1");   //在try代码块中调用checkNum函数
}
return true;
    }
//捕获异常
catch(Exception $e){    //catch代码块接受到该异常,并创建一个包含异常信息的对象($e)
echo '异常信息:'.$e->getMessage();  //输出来自该异常的错误消息
}
?>


2,自定义的异常处理器

eg:
<body>
<?php
class customException extends Exception{

public function errorMessage(){
//错误消息
$errorMsg='异常发生的行:'.$this->getLine().'in'.$this->getFile().':<b>'.$this->getMessage().'</b>不是一个有效的邮箱地址';
return $errorMsg;
    }
}
$email ="someone@example.321com";
try{
//检查是否符合条件
if(filter_var($email.FILTER_VALIDATE_EMAIL)==FALSE){
//如果邮箱地址无效,则抛出异常
throw new customException ($email);
    }
 }
catch  (customException $e){
//显示自定义的消息
echo $e->errorMessage();
}
?>

3,处理多个异常
<?php
class customException extends Execption{
public function errorMessage(){
//定义错误信息
$errorMsg ='错误消息的行'.$this->getLine().'in'.$this->get->getFile().':<b>'.$this->getMessage().'</b>'不是一个有效的邮箱地址';

return $errorMsg;
    }
}
$email ="someone@yahoo.com";
try{
//检查是否符合条件
if(filter_var($email,FILTER_VALIDATE_EMAIL==FALSE){
//如果邮箱地址无效,则抛出异常
throw new customException ($email);
   }
//检查邮箱是否是雅虎邮箱
if (strpos($email,"yahoo")!==FALSE){
throw new Exception ("$email 是一个雅虎邮箱");
   }
}
catch(customExcepiton $e){
echo $e->errorMessage();
    }
catch(Exception $e){
echo $e->getMessage();
}
?>

4,设置顶层异常处理器

eg:
<?php
function myException ($exception){   //定义顶层的异常处理程序

echo "<b>异常是:</b>",$exception->getMessage();
}
set_exception_handler('myException');
throw new Exception('正在处理未被捕获的异常');    //抛出异常信息
?>

eg:
通过打开文件的实例介绍意外的处理方法和技巧
<?php
$DOCUMENT_ROOT =$_SERVER['DOVUMENT_ROOT']; //定义变量
@$fp =fopen("$DOCUMENT_ROOT/book.txt",'rb');   //打开指定文件book.txt
//判断文件是否存在,不存在则抛出异常
try{
if(!$fp){
throw new Exception ("文件路径有误或找不到文件。");
    }
}catch(Exception $exception){
echo $exception ->getMessage();
echo "在文件".$exception->getFile()."的".$exception->getLine()."行。<br/>";
}
@fclose($fp);   //屏蔽错误信息
?>
 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值