1.php的错误捕获:try{} catch(exception $e) { echo $e->getmessage();}句型格式对于错误的调试和控制帮助是非常大的。
<?php class test { static public function atest($val) { if($val>1) throw new Exception("Parms greater than 1"); if($val<1&&$val>0) throw new Exception("error"); echo $val; } } try { $val = 0.5; test::atest($val); } catch (Exception $e) { exit(json_encode(array("err_code"=>"500","err_msg"=>$e->getMessage()))); }?> 优势:主要用来捕获系统级的错误(业务逻辑层面的错误一般不用捕获)这样避免了方法的污染
ps:捕获可以获取到的信息包括getFile() 获取文件 getLine()获取出错的行 getMessage() 获取插入的提示报错信息。框架机制中,一般都需要提供捕获机制
2.如果不用try{}catch(Exception $e){}句型
PHP里提供了一个set_exception_handler()的简化写法
<?php function test1($e) { echo $e->getMessage();}set_exception_handler('test1');throw new Exception("test");?>3.另外一个错误提示的函数(和set_exception_handler()相似的用法)
<?phpfunction test() {echo " XPHP notice error";}set_error_handler("test");test?> 该段代码会报错XPHP notice error
set_error_handler(error_function,error_types)
参数 描述
error_function 必需。规定发生错误时运行的函数。
error_types 可选。规定在哪个错误报告级别会显示用户定义的错误。默认是 "E_ALL"。
4.另外一个常用到的框架函数
register_shutdown_function('endRecord');
脚本运行完成后需要记录的动作
<?phpfunction endRecord() {echo "PHP Script End";}register_shutdown_function('endRecord');?>
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号