How to Try Catch in symfony

user3383675

Situation:

//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 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?

Jan Prieser

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

Thats a difference, the namespace matters.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Try Catch in symfony

From Dev

How to know what to catch with Try/Catch Block

From Dev

How to try/catch all exceptions

From Dev

How to apply try and catch on a function?

From Dev

How to catch Exception in symfony 2?

From Dev

How to catch Exception in symfony 2?

From Dev

does php try catch catches mongodb and symfony errors?

From Dev

try catch orm doctrine on symfony3.3 syntax

From Dev

how exceptions(try, catch) work in php?

From Dev

How to unit test try-catch block?

From Dev

How does `try / catch` work in details

From Dev

How handling error of json decode by try and catch

From Dev

How to return a value from try, catch, and finally?

From Dev

how the Try catch finally block is executed by JVM

From Dev

How to Initialize an InputStream Before Try/Catch Block

From Dev

How to handle Automapper Exceptions (try/catch)

From Dev

How to add try catch block in jquery plugin

From Dev

How to log errors in try/catch statement laravel?

From Dev

Laravel - How to a try/catch in blade view?

From Dev

How to continue running program with try and catch

From Dev

How to use try catch in a switch in a while

From Dev

how to use try and catch in this code (java)

From Dev

How to use nested try/catch in python?

From Dev

How to Initialize an InputStream Before Try/Catch Block

From Dev

How to return a value on Clojure try/catch

From Dev

How to log errors in try/catch statement laravel?

From Dev

How try catch order is executed in JavaScript?

From Dev

How do I catch MySQL errors in a try catch block?

From Dev

Try-Catch, How to make the program catch an unrecognised value