我有一个非常简单的函数来检查一个实体是否存在于一个bundle中:
public function checkExists($bundle, $val)
{
try{
$this->em->getRepository($bundle.':'.$val);
}catch (MappingException $e){
return false;
}
return true;
}
我有以下几种情况:
Input | Expected | Actual
'AppBundle', 'Company' | true | true
'AppBundle', 'NONEXISTANT' | false | false (MappingException caught)
'NONEXISTANT', 'Company' | false | 500 (ORMException not caught)
'NONEXISTANT', 'NONEXISTANT' | false | 500 (ORMException not caught)
所以我发现问题在于抛出了不同的异常,但是如果一个部分不存在的情况下如何返回false呢?在symfony中是否有一种“通用”的捕获异常的方式,如
catch(Exception $e)
,而use Symfony\Component\Config\Definition\Exception\Exception;
无法捕获它。