Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - try, catch, and retry

Tags:

php

try-catch

Sometimes my code breaks and it is out of my control

How would I do the following?

try {
//do my stuff
}
catch {
//sleep and try again
}

The code isn't that much, so it's all one function, so I didn't want to make and call another function if I didn't have to

like image 628
user3869692 Avatar asked Jul 28 '14 18:07

user3869692


People also ask

Does PHP have try catch?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

How does try catch work in PHP?

The “try” block is executed and an exception is thrown if the denominator is zero or negative number. The “catch” block catches the exception and displays the error message. The flowchart below summarizes how our sample code above works for custom types of exceptions.


2 Answers

You can try something like this:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

$NUM_OF_ATTEMPTS = 5;
$attempts = 0;

do {

    try
    {
        executeCode();
    } catch (Exception $e) {
        $attempts++;
        sleep(1);
        continue;
    }

    break;

} while($attempts < $NUM_OF_ATTEMPTS);

function executeCode(){
    echo "Hello world!";
}

Here, we perform a do...while loop so that the code is executed at least once. If the executeCode() function experiences an error, it will throw an Exception which the try...catch block will capture. The catch block will then increment the variable $attempt by one and call continue to test the while condition for the next iteration. If there have already been five attempts, the loop will exit and the script can continue. If there is no error, i.e. the continue statement from the catch block is not executed, the loop will break, thus finishing the script.

Note the use of the set_error_handler function taken from here. We do this so that all errors within the executeCode() function are caught, even if we don't manually throw the errors ourselves.

If you believe your code may fail numerous times, the sleep() function may be beneficial before the continue statement. 'Slowing' down the possibly infinite loop will help with lower your CPU Usage.

It is not a good idea to have a script run infinitely until it is successful, since an error that is present in the first 100 iterations of a loop, is unlikely to ever be resolved, thus causing the script to 'freeze' up. More oft than not, it is better to re-evaluate the code that you would like run multiple times in the case of an error, and improve it to properly handle any errors that come its way.

like image 145
noahnu Avatar answered Oct 22 '22 09:10

noahnu


Simply :

function doSomething($params, $try = 1){
    try{
        //do something
        return true;
    }
    catch(Exception $e){
        if($try <5){
             sleep(10);
             //optionnaly log or send notice mail with $e and $try
             doSomething($params, $try++);
        }
        else{ 
             return false;
        }
    }
}
like image 10
Reign.85 Avatar answered Oct 22 '22 10:10

Reign.85