千家信息网

怎么用php写一个异常处理程序

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章主要介绍"怎么用php写一个异常处理程序",在日常操作中,相信很多人在怎么用php写一个异常处理程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么用php写
千家信息网最后更新 2025年02月01日怎么用php写一个异常处理程序

这篇文章主要介绍"怎么用php写一个异常处理程序",在日常操作中,相信很多人在怎么用php写一个异常处理程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么用php写一个异常处理程序"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

直接上代码

'."[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line']."\n";file_put_contents('./log/'.date("Ymd").'shutdownlog.txt',$error,FILE_APPEND);//友好提示用户ob_end_clean();die('对不起,我出错了!');}}register_shutdown_function('shutdownHandler');}if(!defined('DEBUG')){ function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0){//写日志$exception = new \ErrorException($errstr, 0, $errno, $errfile, $errline);$msg = strval(date("Y-m-d h:i:s")).'=>'.'Type:'.getErrTypeName($errno).' '.getMsg($exception);file_put_contents('./log/'.date("Ymd").'error.txt',$msg,FILE_APPEND);switch ($errno){case E_NOTICE:return ;case E_DEPRECATED:return;}throw $exception;}function getErrTypeName($errno){switch ($errno){case E_NOTICE:return 'E_NOTICE' ;case E_DEPRECATED:return 'E_DEPRECATED';default:return $errno;}}function exceptionHandler($ex){$msg = strval(date("Y-m-d h:i:s")).'=>'.getMsg($ex);file_put_contents('./log/'.date("Ymd").'exception.txt',$msg,FILE_APPEND);}function getMsg($exception){//获取最准确的异常 while($exception->getPrevious())$exception = $exception->getPrevious();$msg = ' Message: '.$exception->getMessage();$msg .= ' File: '.$exception->getFile().':'.$exception->getLine()."\n";return $msg;}set_error_handler('errorHandler',E_ALL);set_exception_handler('exceptionHandler');}?>

到此,关于"怎么用php写一个异常处理程序"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0