Chad Johnson
Chad Johnson

Reputation: 21895

Why do I get an "Uncaught exception" error?

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

Upvotes: 2

Views: 438

Answers (2)

jitter
jitter

Reputation: 54605

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.

Upvotes: 2

Steven Mercatante
Steven Mercatante

Reputation: 25295

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?

Upvotes: 0

Related Questions