循环内的 PHP try-catch 块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4673483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:56:29  来源:igfitidea点击:

PHP try-catch block inside loop

phpperformancetry-catch

提问by All Workers Are Essential

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?

如果打算在发生异常时循环结束,那么将 try-catch 块放在循环中是否效率较低,而不是在 php 中使用 try-catch 包装循环?还是本质上没有区别?

EDIT:

编辑:

i.e.,

IE,

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

versus:

相对:

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

回答by Peter Bailey

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

这完全取决于失败的性质,以及您打算在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 insidea 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 outsidethe 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

回答by Craige

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?

这完全取决于您如何使用 try-catch?如果抛出异常,继续循环遍历主题是否安全?

回答by Pekka

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

很可能没有区别。这个级别的优化在像 PHP 这样的解释性语言中通常没有任何意义。

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

在大多数情况下,您的逻辑将要求您无论如何都将块放入循环中。否则,即使发生错误,循环也会继续。

回答by Carlos Valenzuela

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

我不知道我是否解释得很好,如果你明白,请告诉我