繁体   English   中英

如何捕获PHP中从file_get_contents()引发的异常?

[英]How to catch the Exception thrown from file_get_contents() in PHP?

这是我的代码:

if(!$fileContents = file_get_contents($pathToXMLFile,true))
{
  throw new RuntimeException("Could not open .xml file");
}

我想将此错误作为Exception捕获,以便我可以适当地处理它。 但是,在我得到更改以捕获它之前,脚本似乎中止了。 这有什么解决方法吗?

PS:当我尝试这样做时,我只会失败而无法打开流。 没有此类文件或目录错误消息。

提前致谢

这会将所有错误,警告和通知转换为异常:

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exceptions_error_handler');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM