如何捕获 ContextErrorException

How to catch ContextErrorException

如何在 Symfony 中捕获 ContextErrorException

我明白了

ContextErrorException: Catchable Fatal Error: Object of class DateTime could not be converted to string

当我尝试

try {
   $html .= strval($value);
}
catch(ContextErrorException $e){ }

为什么抓不到?

您必须在 symfony 中使用正确的命名空间

try {
    // code here
} catch (\Symfony\Component\Debug\Exception\ContextErrorException $e) {
    // do something with the exception
}

我用 symfony 2.8 试过了,它成功了。