8

Found this gem in my news feed yesterday and can't get it off my mind: catch exception, sleep, goto try-block and, well, try again:

$attempts = 0;
start:

try {
$attempts++;
// (...)
} catch (Exception $e) {
if ($attempts <= 10) {
usleep(100000);
goto start;
)
}

Did we go full-circle? Is "goto" the next big thing? Is this a late april fools joke? What am I missing here? This can't be real.

Source: https://blog.frankdejonge.nl/back-t...

Comments
  • 7
    There are something in this world even php shouldn't allow.

    https://php.net/manual/en/...

    Welcome to the course, our first module is: How to cause an infinite loop 101.
  • 0
    I'm going to be honest, I thought that the goto was in the try which would've caused an infinite loop. Using a goto and an if instead of a while in 2021 in the middle of an exception catch is almost as awful though.
  • 1
    @C0D4 why would it cause an infinite loop. Shouldn't the "attempt" variable increment on each try and jump over on 10th?
  • 1
    @HitWRight loose the attempts check because some dev forgot how loops work, or move it to down after the exception throwing code, for example:

    try{
    someThingThatThrowsException();
    attempts++;
    }
    catch(Exception $e){
    //check and restart loop
    }

    and the check wouldn't exceed 10.

    It doesn't take much to make that break.
Add Comment