php exceptions - throw/catch not working across loops

570 Views Asked by At

This should so work. Please tell me I'm doing something wrong.

Here's the code:

<?php
try {
  echo "start";
  throw new Exception("test");
  for ($index=0; $index < 1; $index++) {
    echo "loop";
  }
} catch ( Exception $e ){
  echo "caught $e";
}
?>

I'm getting an uncaught exception, if I move the throw inside the loop it works fine, but that defeats my purpose. I could hack in separate try/catch blocks, but thats hackish.

I've tried it on a 5.2.13 and 5.2.17 server. Current production is at 5.2.17, and no I haven't tested it on 5.3.8 because I'd have to get with my host to upgrade, and retest a lot of code.

Any love?

P.S. I do know the difference between an exception and an uncaught exception. But here's the result:

start
Fatal error: Uncaught exception 'Exception' with message 'test' in C:\UniServer\www\admin\water_ws\test.php:4 Stack trace: #0 {main} thrown in C:\UniServer\www\admin\water_ws\test.php on line 4
2

There are 2 best solutions below

0
On BEST ANSWER

As landons suggested, it was eAccelerator's "optimizing" that optimized my catch block away. Once I disabled eAccelerator it works as expected.

1
On

why do you echo $e object? try this:

try {
    throw new Exception("test");
    for ($index=0; $index < 1; $index++) {
        echo "loop";
    }
} catch ( Exception $e ){
    echo $e->getMessage();
}