[php] try - catch exceptiong handler
2015-10-09 19:09
881 查看
//http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning One possibility is to set your own error handler before the call and restore the previous error handler later with restore_error_handler(). set_error_handler(function() { /* ignore errors */ }); dns_get_record(); restore_error_handler(); You could build on this idea and write a re-usable error handler that logs the errors for you. set_error_handler([$logger, 'onSilencedError']); dns_get_record(); restore_error_handler(); Turning errors into exceptions You can use set_error_handler() and the ErrorException class to turn all php errors into exceptions. set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) { // error was suppressed with the @-operator if (0 === error_reporting()) { return false; } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }); try { dns_get_record(); } catch (ErrorException $e) { // ... } The important thing to note when using your own error handler is that it will bypass the error_reporting setting and pass all errors (notices, warnings, etc.) to your error handler. You can set a second argument on set_error_handler() to define which error types you want to receive, or access the current setting using ... = error_reporting() inside the error handler.
相关文章推荐
- matplotlib文章
- yii源码分析I、II
- PHP命名空间和自动加载初探
- laravel5项目安装debugbar
- php sprintf函数 功能很强大滴
- php常用函数汇总
- yii2.0用户登陆
- 很早以前闲着没事好玩写了个php自动加载的类,仅供参考
- php中mysqli 处理查询结果集的几个方法
- php 上传文件到远程服务器
- sntp协议简介
- debian 下,搭建vsftpd 服务器 ---本地用户模式
- phpstorm汉化
- ContentProvide 应用之间的通信
- PHPSTORM/IntelliJ IDEA 常用 设置配置优化
- 在thinkphp框架既然系统已经有了model为什么还需要创建自己的model
- thinkphp修改数据表时怎么实现字段自加1
- phpStorm 配置关联php手册
- PHP学习日记(一)——类、函数的使用
- php中foreach赋值