php错误处理和异常处理机制
添加时间:2014-7-4 3:17:21
添加:
思海网络
当我们开发程序时,有时候程序出现了问题,我们就可以用以下几种办法找出错误。
开发阶段:开发时输出所有的错误报告,有利于我们进行程序调试
运行阶段:我们不要让程序输出任何一种错误报告(不能让用户看到(包括懂技术, 不懂技术的人))
将错误报告写入日志中
一、指定错误报告 error_reporting = E_LL
二、关闭错误输出 display_errors = Off
三、开启错误日志功能 log_errors = On
1. 默认如果不指定错误日志位置,则默认写WEB服务器的日志中
2. 为error_log选项指定 一个文件名(可写)
3. 写入到操作系统日志中error_log=syslog
以下代码示例
";
?>
当然php还提供了函数error_get_last()来获得错误信息
函数定义和用法
error_get_last()函数获取最后发生的错误。 该函数以数组的形式返回最后发生的错误。 返回的数组包含 4 个键和值: [type] - 错误类型 [message] - 错误消息 [file] - 发生错误所在的文件 [line] - 发生错误所在的
小例子:
输出:
Array ( [type] => 8 [message] => Undefined variable: test [file] => D:\www\test.php [line] => 2 )
所以这样我们也很方便了。。。这样是不是对调试程序和排查错误的时候很有帮助呢?
这些错误报告级别是错误处理程序旨在处理的错误的不同的类型:
值 常量 描述
2 E_WARNING 非致命的 run-time 错误。不暂停脚本执行。
8 E_NOTICE
Run-time 通知。
脚本发现可能有错误发生,但也可能在脚本正常运行时发生。
256 E_USER_ERROR 致命的用户生成的错误。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_ERROR。
512 E_USER_WARNING 非致命的用户生成的警告。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_WARNING。
1024 E_USER_NOTICE 用户生成的通知。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_NOTICE。
4096 E_RECOVERABLE_ERROR 可捕获的致命错误。类似 E_ERROR,但可被用户定义的处理程序捕获。(参见 set_error_handler())
8191 E_ALL
所有错误和警告,除级别 E_STRICT 以外。
(在 PHP 6.0,E_STRICT 是 E_ALL 的一部分)
php异常处理机制
定义:
异常处理: 意外,是在程序运行过程中发生的意料这外的事,使用异常改变脚本正常流程
语法格式:
try
{ //...}
catch(Exception $e)
{ //...}
PHP中try{}catch{}是异常处理.
将要执行的代码放入TRY块中,如果这些代码执行过程中某一条语句发生异常,则程序直接跳转到CATCH块中,由$e收集错误信息和显示.
PHP中try{}catch{}语句
为了进一步处理异常,我们需要使用PHP中try{}catch{}----包括Try语句和至少一个的catch语句。任何调用 可能抛出异常的方法的代码都应该使用try语句。Catch语句用来处理可能抛出的异常。
例子:
我写一段代码:
自己定义一个异常类
作用:就是写一个或多个方法解决当发生这个异常时的处理方式
1. 自己定义异常类,必须是Exception(内置类)的子类, 可以查看PHP手册里面Exception(内置类)的使用方法
2. Exception类中的只有构造方法和toString()可以重写, 其它都final
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
?>
1. 如果try中代码没有问题,则将try中代码执行完后就到catch后执行
2. 如果try中代码有异常发生,则抛出一个异常对象(使用throw),抛出给了catch中的参数, 则在try中代码就不会再继续执行下去 直接跳转到catch中去执行, catch中执行完成, 再继续向下执行
注意: 提示发生了什么异常,这不是主要我们要做事,需要在catch中解决这个异常, 如果解决不了,则出去给用户在下面代码中,如果我没有这个TMP.TXT文件的话,就会抛出异常了。
如果有异常,我们调用OPEN方法就可以解决这个异常了。
getMessage()."
"; //getMessage() 是PHP里面内置的方法,可以直接调用
$file=$e->open();
}
下面将代码进行整理以及多个异常处理方法:
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "处理demo发生的异常
";
}
}
class TestException extends Exception {
function pro(){
echo "这里处理test发生的异常
";
}
}
class HelloException extends Exception {
}
class MyClass {
function openfile(){
$file=@fopen("tmp.txt", "r");
if(!$file)
throw new OpenFileException("文件打开失败");
}
function demo($num=0){
if($num==1)
throw new DemoException("演示出异常");
}
function test($num=0){
if($num==1)
throw new TestException("测试出错");
}
function fun($num=0){
if($num==1)
throw new HelloException("###########");
}
}
try{
echo "11111111111111
";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222
";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."
";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."
";
}
var_dump($file);
echo "444444444444444444444
";
User Access Verification
Password:
Router>enable
Password:
Router#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is 221.4.162.254 to network 0.0.0.0
119.0.0.0/32 is subnetted, 160 subnets
S 119.63.36.108 is directly connected, Null0
S 119.63.39.111 is directly connected, Null0
S 119.63.38.107 is directly connected, Null0
S 119.63.34.111 is directly connected, Null0
S 119.63.36.106 is directly connected, Null0
S 119.63.36.107 is directly connected, Null0
S 119.63.37.100 is directly connected, Null0
S 119.63.36.101 is directly connected, Null0
S 119.63.39.98 is directly connected, Null0
S 119.63.36.98 is directly connected, Null0
S 119.63.38.127 is directly connected, Null0
S 119.63.39.123 is directly connected, Null0
S 119.63.39.122 is directly connected, Null0
S 119.63.36.117 is directly connected, Null0
S 119.63.34.112 is directly connected, Null0
S 119.63.39.117 is directly connected, Null0
S 119.63.37.119 is directly connected, Null0
S 119.63.37.118 is directly connected, Null0
S 119.63.34.118 is directly connected, Null0
S 119.63.39.115 is directly connected, Null0
S 119.252.246.98 is directly connected, Null0
S 119.63.36.77 is directly connected, Null0
S 119.63.39.78 is directly connected, Null0
S 119.63.36.79 is directly connected, Null0
S 119.252.245.91 is directly connected, Null0
S 119.63.36.75 is directly connected, Null0
S 119.63.34.66 is directly connected, Null0
S 119.63.38.71 is directly connected, Null0
S 119.63.37.65 is directly connected, Null0
S 119.63.34.69 is directly connected, Null0
S 119.63.36.93 is directly connected, Null0
S 119.63.34.88 is directly connected, Null0
S 119.63.38.93 is directly connected, Null0
S 119.63.36.89 is directly connected, Null0
S 119.63.36.86 is directly connected, Null0
S 119.63.39.82 is directly connected, Null0
S 119.63.39.80 is directly connected, Null0
S 119.63.38.47 is directly connected, Null0
S 119.63.38.42 is directly connected, Null0
S 119.63.36.42 is directly connected, Null0
S 119.63.38.41 is directly connected, Null0
S 119.63.37.37 is directly connected, Null0
S 119.63.38.34 is directly connected, Null0
S 119.63.34.39 is directly connected, Null0
S 119.63.36.35 is directly connected, Null0
S 119.63.39.32 is directly connected, Null0
S 119.63.38.62 is directly connected, Null0
S 119.63.38.63 is directly connected, Null0
S 119.252.245.45 is directly connected, Null0
S 119.63.39.60 is directly connected, Null0
S 119.63.39.58 is directly connected, Null0
S 119.63.36.58 is directly connected, Null0
S 119.63.39.55 is directly connected, Null0
S 119.63.39.52 is directly connected, Null0
S 119.63.39.51 is directly connected, Null0
S 119.63.37.51 is directly connected, Null0
S 119.63.38.14 is directly connected, Null0
S 119.63.34.9 is directly connected, Null0
S 119.63.36.8 is directly connected, Null0
S 119.63.37.9 is directly connected, Null0
S 119.63.40.6 is directly connected, Null0
S 119.63.40.8 is directly connected, Null0
S 119.63.37.4 is directly connected, Null0
S 119.63.38.3 is directly connected, Null0
S 119.63.36.2 is directly connected, Null0
S 119.63.36.29 is directly connected, Null0
S 119.63.38.31 is directly connected, Null0
S 119.63.36.30 is directly connected, Null0
S 119.63.34.30 is directly connected, Null0
S 119.63.39.27 is directly connected, Null0
S 119.63.37.25 is directly connected, Null0
S 119.63.36.27 is directly connected, Null0
S 119.63.36.20 is directly connected, Null0
S 119.63.35.18 is directly connected, Null0
S 119.63.38.20 is directly connected, Null0
S 119.63.36.22 is directly connected, Null0
S 119.63.37.22 is directly connected, Null0
S 119.63.39.20 is directly connected, Null0
S 119.63.35.16 is directly connected, Null0
S 119.63.37.17 is directly connected, Null0
S 119.63.39.19 is directly connected, Null0
S 119.63.38.16 is directly connected, Null0
S 119.63.34.20 is directly connected, Null0
S 119.63.33.23 is directly connected, Null0
S 119.63.37.239 is directly connected, Null0
S 119.63.37.238 is directly connected, Null0
S 119.63.38.234 is directly connected, Null0
S 119.63.34.237 is directly connected, Null0
S 119.63.34.225 is directly connected, Null0
S 119.63.38.226 is directly connected, Null0
S 119.252.246.239 is directly connected, Null0
S 119.63.35.254 is directly connected, Null0
S 119.252.245.232 is directly connected, Null0
S 119.63.34.243 is directly connected, Null0
S 119.63.36.246 is directly connected, Null0
S 119.63.39.244 is directly connected, Null0
S 119.63.39.243 is directly connected, Null0
S 119.63.36.205 is directly connected, Null0
S 119.63.36.206 is directly connected, Null0
S 119.63.34.204 is directly connected, Null0
S 119.63.36.203 is directly connected, Null0
S 119.63.36.197 is directly connected, Null0
S 119.63.38.199 is directly connected, Null0
S 119.63.34.198 is directly connected, Null0
S 119.63.38.194 is directly connected, Null0
S 119.63.36.220 is directly connected, Null0
S 119.63.39.221 is directly connected, Null0
S 119.63.34.217 is directly connected, Null0
S 119.63.36.217 is directly connected, Null0
S 119.63.39.217 is directly connected, Null0
S 119.63.36.218 is directly connected, Null0
S 119.63.39.215 is directly connected, Null0
S 119.63.36.213 is directly connected, Null0
S 119.63.39.212 is directly connected, Null0
S 119.63.38.213 is directly connected, Null0
S 119.63.38.209 is directly connected, Null0
S 119.63.39.175 is directly connected, Null0
S 119.63.36.174 is directly connected, Null0
S 119.63.39.172 is directly connected, Null0
S 119.63.38.173 is directly connected, Null0
S 119.63.36.168 is directly connected, Null0
S 119.63.39.170 is directly connected, Null0
S 119.63.38.171 is directly connected, Null0
S 119.63.38.168 is directly connected, Null0
S 119.63.39.168 is directly connected, Null0
S 119.63.39.167 is directly connected, Null0
S 119.63.34.163 is directly connected, Null0
S 119.63.34.160 is directly connected, Null0
S 119.63.38.165 is directly connected, Null0
S 119.63.38.190 is directly connected, Null0
S 119.63.36.184 is directly connected, Null0
S 119.63.38.187 is directly connected, Null0
S 119.63.39.186 is directly connected, Null0
S 119.252.245.167 is directly connected, Null0
S 119.63.39.182 is directly connected, Null0
S 119.63.37.177 is directly connected, Null0
S 119.63.38.179 is directly connected, Null0
S 119.252.245.160 is directly connected, Null0
S 119.63.37.143 is directly connected, Null0
S 119.63.39.138 is directly connected, Null0
S 119.63.39.137 is directly connected, Null0
S 119.63.39.136 is directly connected, Null0
S 119.63.36.139 is directly connected, Null0
S 119.63.38.132 is directly connected, Null0
S 119.63.39.132 is directly connected, Null0
S 119.63.38.133 is directly connected, Null0
S 119.63.37.129 is directly connected, Null0
S 119.63.39.130 is directly connected, Null0
S 119.63.37.131 is directly connected, Null0
S 119.63.36.131 is directly connected, Null0
S 119.63.38.158 is directly connected, Null0
S 119.63.34.155 is directly connected, Null0
S 119.63.34.152 is directly connected, Null0
S 119.63.36.158 is directly connected, Null0
S 119.63.38.157 is directly connected, Null0
S 119.63.34.159 is directly connected, Null0
S 119.63.34.157 is directly connected, Null0
S 119.63.36.149 is directly connected, Null0
S 119.63.36.150 is directly connected, Null0
S 119.63.36.151 is directly connected, Null0
221.4.1.0/32 is subnetted, 1 subnets
S 221.4.1.201 [1/0] via 221.4.162.254
112.0.0.0/32 is subnetted, 1 subnets
S 112.91.22.171 is directly connected, Null0
221.4.162.0/25 is subnetted, 1 subnets
C 221.4.162.128 is directly connected, FastEthernet0/0
183.60.0.0/32 is subnetted, 6 subnets
S 183.60.233.120 is directly connected, Null0
S 183.60.201.106 is directly connected, Null0
S 183.60.203.120 is directly connected, Null0
S 183.60.205.2 is directly connected, Null0
S 183.60.203.14 is directly connected, Null0
S 183.60.107.94 is directly connected, Null0
S* 0.0.0.0/0 [1/0] via 221.4.162.254
Router#
关键字:php、日志、机制
新文章:
- CentOS7下图形配置网络的方法
- CentOS 7如何添加删除用户
- 如何解决centos7双系统后丢失windows启动项
- CentOS单网卡如何批量添加不同IP段
- CentOS下iconv命令的介绍
- Centos7 SSH密钥登陆及密码密钥双重验证详解
- CentOS 7.1添加删除用户的方法
- CentOS查找/扫描局域网打印机IP讲解
- CentOS7使用hostapd实现无AP模式的详解
- su命令不能切换root的解决方法
- 解决VMware下CentOS7网络重启出错
- 解决Centos7双系统后丢失windows启动项
- CentOS下如何避免文件覆盖
- CentOS7和CentOS6系统有什么不同呢
- Centos 6.6默认iptable规则详解