php 捕获warning,php错误及异常捕捉

1、php 错误常量及含义:

常量

描述

1

E_ERROR

Fatal run-time errors. Errors that can not be recovered from.

Execution of the script is halted 致命错误

2

E_WARNING

Non-fatal run-time errors. Execution of the script is not

halted

警告不中断

4

E_PARSE

Compile-time parse errors. Parse errors should only be

generated by the parser语法解析错误

8

E_NOTICE

Run-time notices. The script found something that might be an

error, but could also happen when running a script normally

能在脚本正常运行时发生

16

E_CORE_ERROR

Fatal errors at PHP startup. This is like an E_ERROR in the PHP

core

在PHP启动时发生的致命错误

32

E_CORE_WARNING

Non-fatal errors at PHP startup. This is like an E_WARNING in

the PHP core

报告在PHP启动时发生的非致命性错误

64

E_COMPILE_ERROR

Fatal compile-time errors. This is like an E_ERROR generated by

the Zend Scripting Engine

报告致命的编译时错误

128

E_COMPILE_WARNING

Non-fatal compile-time errors. This is like an E_WARNING

generated by the Zend Scripting Engine

报告非致命的编译时错误

256

E_USER_ERROR

Fatal user-generated error. This is like an E_ERROR set by the

programmer using the PHP function trigger_error()

报告用户引发的致命错误信息(由set_error_handle捕捉及trigger_error触发)

512

E_USER_WARNING

Non-fatal user-generated warning. This is like an E_WARNING set

by the programmer using the PHP function trigger_error()

报告用户引发的非致命错误信息(由set_error_handle捕捉及trigger_error触发)

1024

E_USER_NOTICE

User-generated notice. This is like an E_NOTICE set by the

programmer using the PHP function trigger_error()

报告用户引发的注意消息

2048

E_STRICT

Run-time notices. PHP suggest changes to your code to help

interoperability and compatibility of the code

PHP版本可移植性建议

4096

E_RECOVERABLE_ERROR

Catchable fatal error. This is like an E_ERROR but can be caught

by a user defined handle (see also set_error_handler())

PHP 5.2 也新增了一个错误报告级别:E_RECOVERABLE_ERROR 它的表现就和所有 PHP

版本中的E_ERROR一样会导致程序中止 ,这种错误是可以被捕捉的,例如set_error_handler()

8191

E_ALL

All errors and warnings, except level E_STRICT (E_STRICT will be

part of E_ALL as of PHP 6.0)

报告所有错误和报告

2、php错误及异常捕捉:

a) set_error_handler

一般用于捕捉 E_NOTICE

、E_USER_ERROR、E_USER_WARNING、E_USER_NOTICE

不能捕捉:

E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR

and E_COMPILE_WARNING

b) set_exception_handler

它在有异常发生但无代码捕获时被自动调用。被调用之后程序立即终止。捕捉 throw

异常

c) register_shutdown_function

执行机制是:php把要调用的函数调入内存。当页面所有PHP语句都执行完成时,再调用此函数

for example:

date_default_timezone_set('Asia/Shanghai');

register_shutdown_function('my_exception_handler');

function my_exception_handler()

{

if($e =

error_get_last()) {

//$e['type']对应php_error常量 $message = '';

$message .=

"出错信息:\t".$e['message']."\n\n";

$message .=

"出错文件:\t".$e['file']."\n\n";

$message .=

"出错行数:\t".$e['line']."\n\n";

$message .=

"\t\t请工程师检查出现程序".$e['file']."出现错误的原因\n";

$message .=

"\t\t希望能引起你对程序开发过程当中出现错误原因的重视\n";

$message .=

"\t\t希望能您早点解决故障出现的原因\n";

//sendemail to

}

}

便于程序、及使用资源的预警 such as memcache exceptions ; redis connect time

out etc.

php.ini当中

display_errors = Off

脚本test.php

E_PARSE

ini_set(‘display_errors’,'On’);

error_reporting(E_ALL);

通过apache nginx 访问结果无内容 code 500

思考下原因

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值