[英]Catch ContextErrorException from Symfony2
I'm reading a XML file in PHP and want to do this within a try catch
block so i can handle exceptions (broken files). 我正在读取PHP中的XML文件,并希望在
try catch
块中执行此操作,以便我可以处理异常(损坏的文件)。
So my code for this looks like this: 所以我的代码看起来像这样:
try{
$xml = simplexml_load_string($file);
} catch (Exception $ex) {
//do stuff
}
But instead of the catch i'm getting this Exception message: 但是我没有得到这个异常消息而不是抓住:
ContextErrorException: Warning: simplexml_load_string(): Entity: line 4284: parser error : Premature end of data in tag Data line 2201
I'm not even sure if this is a real Exception because of the Warning:
right after that, is it a warning and not a Exception? 由于
Warning:
我什至不确定这是否是真正的异常Warning:
在那之后,这是警告而不是异常吗? If it is a Exception, why can't i catch it? 如果是例外,为什么我不能抓住它?
I fixed it by myself by changing the error handler: 我自己通过更改错误处理程序来修复它:
set_error_handler(
function($errno, $errstr, $errfile, $errline, array $errcontext)
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
);
I'm not sure, but i'm guessing that the Exception that i recieved was indeed a warning, so it couldn't be catched. 我不确定,但是我猜我收到的异常确实是警告,因此无法捕获。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.