Yes, just don’t use them. Let the exception bubble up the stack. PHP already handles them quite well. You can use set_exemption_handler for custom exception handling.
With that said, there are two instances where you do want to catch exceptions. That would be in the database connection and catching a duplicate insert error such as could happen on a user registration.
@igor_g Your question is a little vague. One possible answer – but again without fully understanding what you’re trying to achieve – is to keep all your happy-path code together and use just one try-catch block for the whole thing.
OK. Looks like you take in an array of callables, and you want to execute each one until you get to one that doesn’t fail with an exception? Currently you’re doing that with recursion, which is fine. You could also write it as a loop, perhaps like this:
public function execute(array $closures) {
foreach ($closures as $closure) {
try {
return call_user_func($closure);
} catch (\Exception $exception) {
// Suppress and ignore error; try next callable
}
}
// If we get here, then...
throw new Exception('All callables failed.');
}
Problem is that in reality that is not array of closures, that is just some pieces of code. Of course, I can to pack it all in closures, but I find it too complex as alternative for try/catch blocks.
If this is just some pieces of code, then if step 1 fails, why would you want to execute step 2? Does step 2 not depend on step 1 completing successfully?