2

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 ?

4
  • you cause another (unhandled) exception within your catch block (aka raise)... and this is very bad style btw.
    – Najzero
    Dec 13, 2012 at 8:12
  • 2
    You don't should use exceptions to control the flow of your application. Use if / else statements instead.
    – dognose
    Dec 13, 2012 at 8:14
  • Najzero, the checknum() function actually calls a function from a .dll whose source is not available, and 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. Dec 13, 2012 at 8:14
  • dognose, how can i catch the exception then and repeat the function call 1 or 2 more times until it doesn't throw it ? Dec 13, 2012 at 8:15

5 Answers 5

9

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.

2
  • What if we only want to repeat the try block when the exception occurs?
    – Farveaz
    Apr 20, 2018 at 10:49
  • 1
    @Farveaz This is exactly what happens. When an exception occurs, the break is skipped and the loop repeated. Apr 20, 2018 at 11:43
3

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
2
  • 1
    reminds me of goto statement in GWBASIC in school back in 90s.... Should work here. +1.
    – itachi
    Dec 13, 2012 at 8:30
  • You are right .. it was fun those days .... when you write 100 lines and you seen like a god ... :)
    – Baba
    Dec 13, 2012 at 8:38
2

In Addition to my comment: Why are you throwing an exception? Your exception is thrown, if the number is greater than 1 - remove the exception and use something like this:

function checkNum($number) {
    echo $number . "<br>";
    if ($number>1) {
        return false;
    }
    return true;
}

$crashesn = 0;
$number = 2; // number from somewhere.
$failed =false;
while (!$checkNum($number)){
  echo $number." is to large!";
  $crashesn++;

  if ($crashesn > 2){
     //stop retrying it.
     $failed = true;
     break;
  }
}

if (!$failed){
   echo $number." is valid";
}else{
   echo "Failed after 2 retries";
}

However it does NOT make sence to repeat the call if the number is a "fixed" value. It will fail three times or be valid on first run.

Exceptions are for cirital Erros. DON't use them to validate something you can take care of with normal logical expressions.

2
  • the artificial exception throwing was just an example of a function which does throw an exception due to an error. It was not in order to check whether the number was bigger or smaller. Dec 13, 2012 at 9:15
  • @MIrrorMirror okay, didn't know that. Well if you are using an external function, that throws the exception you would have to catch it, that's right. But instead of hammering the function until it works, you should resolve the issue and not simple retrying it. This is work around symptoms instead of resolving the problem. You also should consider to replace any external functions that are not working as expected.
    – dognose
    Dec 13, 2012 at 11:26
1

In your catch , u supposed to have another try catch

try {

          // some code here
   }
    catch(Exception $e) {

          try {
            $crashesn++;
            echo "crashes number:".$crashesn."<br>";
            if ($crashesn <= 2) {
                sleep(1);
                checkNum(2);
            } 

          }
          catch(Exception $e) {

            echo "MESSAGE: " .$e->getMessage();
          }

   }
1
$checked = false;
while ($i < 3 && $checked === false){
    try{
        $checked = checkNum($number);
    }
    catch(Exception $e){
        $i++;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.