Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: repeat function call 2-3 times in catch()

so a function crashes sometimes ( uncaught exception ) and i want to re-call it 2 times when that happens with a delay of 1 seconds let's say. Here's is the code but it doesn't seem to work:

$crashesn = 0;

function checkNum($number) {
    echo $number . "<br>";
    if ($number>1) {
        throw new Exception("Value must be 1 or below");
    }
    return true;
}

try {
    checkNum(2);
    echo 'If you see this, the number is 1 or below';
}

catch(Exception $e) {
    $crashesn++;
    echo "crashes number:".$crashesn."<br>";
    if ($crashesn <= 2) {
        sleep(1);
        checkNum(2);
    } else {
        echo "MESSAGE: " .$e->getMessage();
    }
}

checknum is the function which throws the exception ( here it crashes every time by throwing an exception ). The problem is, when I run this piece of code, i still get on my page an error message

Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in G:\fix\ta_dll\test.php:30 Stack trace: #0 c:\wwwl\test.php(45): checkNum(2) #1 {main} thrown in c:\php\test.php on line 30

instead of a "MESSAGE: ERROR DESCRIPTION". the "crashes number" line only gets printed once, not twice.

anyone knows what I am doing wrong ?

like image 516
MirrorMirror Avatar asked Jan 30 '26 15:01

MirrorMirror


2 Answers

Try with a loop instead

for ($crashes = 0; $crashes < 3; $crashes++) {
    try {
        checkNum(2);
        echo 'If you see this, the number is 1 or below';
        break;
    }
    catch(Exception $e) {
        echo "crashes number:" . $crashes . "<br>";
        if ($crashes < 2) {
            sleep(1);
        } else {
            echo "MESSAGE: " . $e->getMessage();
        }
    }
}

When checkNum() returns properly, the for loop is left with the break. Otherwise, when an exception is thrown, break is skipped and the loop repeats.

like image 129
Olaf Dietsche Avatar answered Feb 02 '26 06:02

Olaf Dietsche


From your comments : it crashes sometimes (due to memory leaks i guess). So I need to auto-repeat it 2 or 3 more times when the crash happens.

Solution 1 : Try using goto ... its old school and can be evil but it might work for you

$crashesn = 0;
START:
try {
    checkNum(2);
    echo 'If you see this, the number is 1 or below';
    break;
} catch ( Exception $e ) {
    $crashesn ++;
    echo "crashes number:" . $crashesn . "<br>";
    if ($crashesn <= 2) {
        sleep(1);
        goto START;
    } else {
        echo "MESSAGE: " . $e->getMessage();
    }
}

Solution 2: just use loop you can also read Performance of try-catch in php

$crashesn = 0;
do {
    try {
        checkNum(2);
        echo 'If you see this, the number is 1 or below';
    } catch ( Exception $e ) {
        $crashesn ++;
        echo "crashes number:" . $crashesn . "<br>";
        if ($crashesn <= 2) {
            sleep(1);
        } else {
            echo "MESSAGE: " . $e->getMessage();
        }
    }
} while ( $crashesn <= 2 );

Both would Output

2
crashes number:1
2
crashes number:2
2
crashes number:3
MESSAGE: Value must be 1 or below
like image 39
Baba Avatar answered Feb 02 '26 06:02

Baba