首页 > 其他 > 详细

如何让你的服务器永远不抛出500

时间:2019-10-29 14:47:09      阅读:95      评论:0      收藏:0      [点我收藏+]

PHP常见错误:

1、调用function参数错误

array_merge([1,2,3],‘‘); -- Warning: array_merge(): Expected parameter 2 to be an array, string given

2、类不存在

Fatal error: Uncaught Error: Class test not found

3、变量未定义

PHP Notice:  Undefined variable

4、数组无匹配下标

PHP Notice:  Undefined index: abc 

5、数据库入库错误

column not found
data too long for name
Invalid datetime format: 1292 Incorrect datetime value: 111 for column update_time at row 1

 

常用的捕获错误方法:

1、try catch捕获数据库错误

try {
    $TalentModelObj->update($talentInfo,array([id,=,$talentId]));
    return array(retcode=>StatusCode::$SUCCESS,data=>$talentId,msg=>编辑候选人信息成功);
} catch (\Throwable $th) {
    return array(retcode=>StatusCode::$ERROR,data=>$th,msg=>编辑失败,错误信息:.json_encode($th));
}

2、注册错误回调函数捕获错误

// 设定报错级别为全部
error_reporting(E_ALL);
// set_error_handler — 设置用户自定义的错误处理函数
set_error_handler([__CLASS__, appError]);
// set_exception_handler — 设置用户自定义的异常处理函数
set_exception_handler([__CLASS__, appException]);
// register_shutdown_function — 注册一个会在php中止时执行的函数,脚本执行完成或者 exit() 后被调用
register_shutdown_function([__CLASS__, appShutdown]);

 


 

1、调用方法参数错误 - 可以用try catch捕获到

function e($a){
    return $a;
}
try {
    $a = e();
} catch (\Throwable $th) {
    print_r($th);
}

2、直接输出未定义变量 - 脚本会异常终止 - 只能用 register_shutdown_function 注册函数回调记录异常信息

try {
    echo $c;
} catch (\Throwable $th) {
    print_r($th);
}

3、数据库错误(列未定义、字段过长等)都可以用try

 

如何让你的服务器永远不抛出500

原文:https://www.cnblogs.com/xuweiqiang/p/11758097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!