Included exceptions

I really don’t believe in that, but…

If there is a followed code…

try {
    // something
} catch (\Exception $exception) {
    try {
        // something else 
    } catch (\Exception $exception) {
        try {
            // something completely different
        } catch (\Exception $exception) {
            // and so on...
        }
    }
}

…is there some elegant way to avoid try/catch blocks including?

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.

I an not sure, but if you mean…

try {

} catch (FooException $foo) {

} catch (BarException $bar) {

}

No, all exceptions are same.

No, that is not what I mean. I mean, delete the try catch blocks.

Could you please post some code example?

How can I post code that shouldn’t exist? If you see try and catch, delete it unless it is one of the two examples I said to use it.

@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.

try {
    // something
    // something else
    // something completely different
} catch (Exception $e) {
    // handle error
}

In my exampe above if // something throws exception, than executed // something else and so on…

In your example if // something throws exception, that just causes error.

I mean, for example. class like…

class Executor
{
    public function execute(array $closures) // \Closure[] $closures
    {
        $closure = array_shift($closures);

        if (empty($closures)) {
            $result = call_user_func($closure);
        } else {
            try {
                $result = call_user_func($closure);
            } catch (\Exception $exception) {
                $result = $this->execute($closures);
            }
        }

        return $result;
    }
}

I had a little hope, that something like that exists in native PHP.

I mean, for example. class like…

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?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.