Re: [lists.php] Re: [PHP-DEV] Continued try blocks
HI,
I also really don't like it.
if you have a try/catch within a for, will it continue the try or for?
(or have I to use "continue 2" to reach the for) At least use another
keyword to make it more clear that you don't want to continue a loop
at the beginning but a catch statement after the exception.
I would have solved your example like this:
try {
try {
$user = $repository->findById(123);
} catch (NotFoundException $e) {
$user = new User();
}
try {
$user->setName($name);
} catch (ValidationException $e) {
$user->setName($this->stripInvalidChars($name));
}
$em->save($user);
return true;
} catch (Exception $e) {
return false;
}
I think that's more clear - but a few lines longer.
And it's probably more save, when the "$em->save($user);" throws a
NotFoundException exception then a new user will be created and then
(without saving) true is returned.
Thread (33 messages)