2

所以一个函数有时会崩溃(未捕获的异常),我想在延迟 1 秒的情况下重新调用它 2 次。这是代码,但它似乎不起作用:

$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 是抛出异常的函数(这里它每次都通过抛出异常而崩溃)。问题是,当我运行这段代码时,我的页面上仍然出现错误消息

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

而不是“消息:错误描述”。“崩溃编号”行只打印一次,而不是两次。

有谁知道我做错了什么?

4

5 回答 5

9

尝试使用循环

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();
        }
    }
}

checkNum()正确返回时,for 循环会留下中断。否则,当抛出异常时,将跳过 break 并重复循环。

于 2012-12-13T08:18:29.323 回答
3

从您的评论中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.

解决方案 1:尝试使用goto ...它的老派,可能是邪恶 的,但它可能对你有用

$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();
    }
}

解决方案2:只需使用循环,您还可以阅读php中try-catch的性能

$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 );

两者都会输出

2
crashes number:1
2
crashes number:2
2
crashes number:3
MESSAGE: Value must be 1 or below
于 2012-12-13T08:22:40.903 回答
2

除了我的评论:你为什么要抛出异常?如果数字大于 1,则会引发您的异常 - 删除异常并使用以下内容:

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";
}

但是,如果号码是“固定”值,则重复呼叫是没有意义的。它将失败三次或在第一次运行时有效。

例外情况是 Cirital Erros。不要使用它们来验证您可以使用普通逻辑表达式处理的事情。

于 2012-12-13T08:24:28.180 回答
1

在你的捕获中,你应该有另一个尝试捕获

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();
          }

   }
于 2012-12-13T08:16:10.800 回答
1
$checked = false;
while ($i < 3 && $checked === false){
    try{
        $checked = checkNum($number);
    }
    catch(Exception $e){
        $i++;
    }
}
于 2012-12-13T08:24:07.617 回答