php中设定一个全局异常处理。全局catch。默认catch。默认异常处理

 1 <?php
 2 
 3     function handleMissedException($e) {
 4             echo "Sorry, something is wrong. Please try again, or contact us if the problem persists. Thanks!";
 5             //error_log($str);//保存一条错误信息字符串到web服务器的error_log文档里。
 6             error_log('Unhandled Exception:' . $e->getMessage() . 'in file' . $e->getFile() . 'on line' . $e->getLine());
 7     }
 8 
 9     set_exception_handler('handleMissedException');
10 
11     //测试抛出一个错误。
12     throw new Exception('just testing');
13 
14     //整个测试程序就这么多。
15     //上下文都没有catch。所以会触发 handleMissedExceotion($e)函数, $e为Exception或Exception的子类

error_log存在web服务器的error_log里。

例如:apache可以在配置文件httpd.conf中搜error_log找到它的位置。

原文地址:https://www.cnblogs.com/sweetXiaoma/p/5996432.html