开发者

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

开发者 https://www.devze.com 2023-03-14 18:23 出处:网络
Here\'s my code: if(!$fileContents = file_get_contents($pathToXMLFile,true)) { throw new RuntimeException(\"Could not open .xml file\");

Here's my code:

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

I would like to catch this error as an Exception so that I can handle it appropriately. However, it seems that the script aborts before I get the change to catch it. Is there any workaround to this?

P.S.: When I try and do that, I only get a faile to open stream. No such file or directory error message.

Thanks in advanc开发者_如何学运维e


This will convert all errors, warnings and notices to exceptions:

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');
0

精彩评论

暂无评论...
验证码 换一张
取 消