栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用file_get_contents进行良好的错误处理

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用file_get_contents进行良好的错误处理

这是一个主意:

function fget_contents() {    $args = func_get_args();    // the @ can be removed if you lower error_reporting level    $contents = @call_user_func_array('file_get_contents', $args);    if ($contents === false) {        throw new Exception('Failed to open ' . $file);    } else {        return $contents;    }}

基本上是的包装器

file_get_contents
。失败将引发异常。为了避免不得不覆盖
file_get_contents
自身,您可以

// change this$dom->load(call_user_func_array('file_get_contents', $args), true); // to$dom->load(call_user_func_array('fget_contents', $args), true);

现在你可以:

try {    $html3 = file_get_html(trim("$link")); } catch (Exception $e) {    // handle error here}

错误抑制(通过使用

@
或通过降低error_reporting级别是一种 有效的
解决方案。这可能引发异常,您可以使用它来处理错误。有很多原因
file_get_contents
可能会生成警告,PHP手册本身建议降低error_reporting:请参见手册



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568664.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号