如何在 symfony 中尝试 Catch [英] How to Try Catch in symfony

查看:19
本文介绍了如何在 symfony 中尝试 Catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

//trollCommand.php
[...]
foreach ($trolltypes as $type) { //$type=={"Frost","RandomBroken","Forest"}
    try {
        $output->writeln($type);
        $troll={"get".$type."TrollType"}();
        $output->writeln("TEST 1");
        $troll->__load();
        $output->writeln("TEST 2");
    } catch (EntityNotFoundException $e) {
        $output->writeln("WARNING: TROLL ENTITY DOES NOT EXIST.");
        continue;
    }
    $output->writeln("TROLLING");
    do_something_with_troll($troll);
}

getFrostTrollType 加载正常,getForestTrollType 也应该加载正常,但在此之前,这是一个问题,getRandomBrokenTrollType() 故意不存在,然后我在控制台看到消息:

getFrostTrollType loads ok, getForestTrollType should be loaded ok too, but before that, it is a problem, getRandomBrokenTrollType() deliberately does not exist, and then I see message in console:

 Frost
 Test 1
 Test 2
 TROLLING
 RandomBroken
 Test 1
 [Doctrine\ORM\EntityNotFoundException]  
 Entity was not found. 
 //[EXIT FROM SCRIPT]
 troll@troll-machine ~/trollSandbox/ $ _

它应该是:警告:巨魔实体不存在.然后继续;但它不会发生

it should be: WARNING: TROLL ENTITY DOES NOT EXIST. and then continue; but it does not happen

如何检查对象的方法是否存在?

How to check existing of a object's method?

推荐答案

Doctrine 抛出的异常称为 Doctrine\ORM\EntityNotFoundException 并且您正在捕获 EntityNotFoundException.

the Exception thrown by Doctrine is called Doctrine\ORM\EntityNotFoundException and you are catching EntityNotFoundException.

那有区别,命名空间很重要.

Thats a difference, the namespace matters.

要调试它,请改为捕获 Exception 并观察实际异常的类型.然后替换它.

to debug this, catch Exception instead and observe the type of the actual exception. then replace it.

这篇关于如何在 symfony 中尝试 Catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆