Php try catch in foreach

Is it less efficient to put a try-catch block inside of a loop as opposed to wrapping the loop with a try-catch in php if it is intended that the loop will end if an exception occurs? Or is there essentially no difference?

EDIT:

i.e.,

foreach (/*...*/) {
    //...
    try {
        //...
    } catch (/*...*/) {
        break;
    }
    //...
}

versus:

try {
    foreach (/*...*/) {
        //...
    }
}

asked Jan 12, 2011 at 20:07

2

That entirely depends on the nature of the failure, and what you intend to do in the catch.

But I'd generalize it like so

  • If you want the loop to exit on Exception, wrap the whole loop
  • If you want the loop to continue, don't

EDIT

Exceptions caught inside a loop don't implicitly break the loop

for ($i = 1; $i < 10; $i++) {
    try {
        if ($i % 3 == 0) {
            throw new Exception('BOOM');
        }
        echo $i;
    } catch (Exception $e) {
        echo "Exception at $i";
    }
    echo PHP_EOL;
}

output:

1
2
Exception at 3
4
5
Exception at 6
7
8
Exception at 9

Whereas those caught outside the loop do

try {
    for ($i = 1; $i < 10; $i++) {
        if ($i % 3 == 0) {
            throw new Exception('BOOM');
        }
        echo $i, PHP_EOL;
    }
} catch ( Exception $e ) {
    echo "Exception at $i";
}

output:

1
2
Exception at 3

answered Jan 12, 2011 at 20:10

Peter BaileyPeter Bailey

104k31 gold badges180 silver badges201 bronze badges

6

That depends entirely on how you are using the try-catch? Is it safe to continue looping through your subject if an exception was thrown?

answered Jan 12, 2011 at 20:09

CraigeCraige

2,8522 gold badges19 silver badges28 bronze badges

There is most likely no difference. Optimization on this level does usually not make any sense in an interpreted language like PHP.

In most cases, your logic will require you to put the block inside the loop anyway. Otherwise, the loop will continue even if an error has occurred.

answered Jan 12, 2011 at 20:09

Php try catch in foreach

PekkaPekka

434k137 gold badges964 silver badges1077 bronze badges

Of course, there's a difference, in the most obvious way int the first case you will only check for errors before entering the loop, if the loop doesn't have exception throwers, leave it that way. In the other hand you will be checking it in each iteration, wich you'll need if you have sentences or methods ... wich can have exceptions.

I'dont know if i explain myself well, let me know if you understand

answered Jan 12, 2011 at 20:11

Php try catch in foreach

Not the answer you're looking for? Browse other questions tagged php performance try-catch or ask your own question.

Can we use try catch in foreach loop?

Foreach loop does not guarantee in-order execution. Sounds weird, almost never occur, but is still possible. If you use foreach for tasks it was created (dataset manipulation), then you might want to place try-catch around it. But as explained, you should try to not catch yourself using try-catch too often.

Does throwing exception break loop PHP?

The loop will break if an exception occurs. if you want to break loop on exception , put try .. catch outside of the loop. good example, thanks!

How do you continue a loop in PHP?

The PHP continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition. The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.