Re: [lists.php] Re: [PHP-DEV] Continued try blocks
| From: | ALeX | Date: | Mon, 29 Apr 2013 15:32:33 +0000 |
| Subject: | Re: [lists.php] Re: [PHP-DEV] Continued try blocks | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-67192@lists.php.net to get a copy of this message | ||
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.