views:

158

answers:

2

I have the following PHP code:

foreach (...) {
  try {
    $Object = MyDataMapper::getById(123);

    if (!$Object->propertyIsTrue()) {
      continue;
    }
  }
  catch (Exception $e) {
    continue;
  }
}

MyDataMapper::getById() will throw an Exception if a database record is not found. Here is the definition of that method:

public static function getById($id) {
  $query = "SELECT * FROM table WHERE id = $id";

  $Connection = Database::getInstance();
  $Statement = $Connection->prepare($query);
  $Statement->execute();

  if ($Statement->rowCount() == 0) {
    throw new Exception("Record does not exist!");
    return null;
  }

  $row = $Statement->fetch();

  return self::create($row);
}

When this code is called for a database record id that does not exist, I get a fatal uncaught exception 'Exception' error.

Why is this? Clearly I am catching the exception... What am I doing wrong?

I am sure an exception is being thrown. Is there something wrong with how I am handling the exception--maybe with the continue?

EDIT

Thanks to help from jitter, the following workaround solves this problem:

if (!$Object->propertyIsTrue()) {
 // Workaround to eAccelerator bug 291 (http://eaccelerator.net/ticket/291).
 $foo = 555;
 continue;
}
A: 

I think it may be because you're calling a static method, but I could be wrong. Is it possible for you to test this by instantiating MyDataMapper and calling the method from the object itself?

Arms
Well, I've done this dozens of times before with other, static mapper methods--so I know that is not the problem.
Chad Johnson
Oh, in that case I would suggest setting up a custom Exception handler (http://us2.php.net/manual/en/function.set-exception-handler.php) so you can look at the trace and do some debugging :)
Arms
I actually have one in place--I receive an email whenever a fatal error is thrown. The trace included points to the code I've outlined in my post.
Chad Johnson
someone mind explaining the down vote on this?
Arms
Wasn't me that down-voted.
Chad Johnson
+2  A: 

What PHP version? -> 5.2.9

Are you using eAccelerator (which version)? -> 0.9.5.3 with ionCube PHP Loader v3.1.34

What kind of exception do you throw? -> normal Exception


There are known problems in certain PHP + eAccelerator versions in regard to try-catch blocks being optimized away.

Check the eAccelerator bug-tracker:

For a starter check the tickets

291 Incorrect handling of exception

314 Exceptions not catched

317 Exception not caught

and try disabling eAccelerator.

jitter
v5.2.9. Yes: v0.9.5.3 with ionCube PHP Loader v3.1.34. Just an instance of Exception. It occurs in the mapper method, which is called in the foreach loop, and so it happens inside the foreach loop when MyDataMapper::getById() is called with an id of a non-existent record.
Chad Johnson
Extended answer
jitter
Thank you very much for your help. eAccelerator issue 291 was the problem. Using the suggested workaround there solved the problem (APPARENTLY!). I've updated my post with the workaround.
Chad Johnson
Wow. Makes me glad I've never used eAccelerator.
Frank Farmer