1

Ok, just a technical question about the code above:

foreach($x as $y){ // or even a simple for()
    try{
        a();
    } catch(\Exception $e){
        // just skip current iteration
        continue;
    } finally {
        c();
    }
}

Since c() is in the finally block it should be always executed, but what about the continue statement?
According to the documentation it appears to be making the finally block being skipped.

So, does c() be executed in case of a() throwing an exception?

1 Answer 1

2

It's simple to discover just using console. Type

php -r 'foreach([1, 2] as $n){try {echo "\n", $n, "\n"; throw new \Exception();} catch (\Exception $e) {continue;} finally {echo "finally has been called";}}'

which is single-string representation of the code

foreach ([1, 2] as $n) {
    try {
        echo "\n", $n, "\n";
        throw new \Exception();
    } catch (\Exception $e) {
        continue;
    } finally {
        echo "finally has been called";
    }
}

you'll get

1
finally has been called
2
finally has been called
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.