0

I have a try-catch block that iterates over a set of records like this:

try {
  foreach ( $json['location'] as $records ) {
    $location = DnaExtractionTable::getInstance()->find($records['id']);
    $location->setName($records['name']);
    $location->setLatitude($records['latitude']);
    $location->setLongitude($records['longitude']);
    $location->setCountryId($records['country_id']);
    $location->setRegionId($records['region_id']);
    $location->setIslandId($records['island_id']);
    $location->setRemarks($records['remarks']);
    $location->save();
  }
}
catch (Exception $e) {
  ...
}

I can catch every exception that is thrown and continue without problems. But I am also trying to "catch" the errors, e.g. when a index does not exist in the $records array.

Is it possible to do that? How I can do it? I've been playing with set_X_handler functions without success.

UPDATE 1:

Following advices from comments and answers, I decided to implement a global error function:

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exceptions_error_handler');

But even if I try to force an error the code does not execute. Since I am developing with Symfony, is there a place to declare that function? Could be Symfony disabling or affecting the set_error_handler function?

UPDATE 2:

Symfony is definitely messing around with my error and exception handlers.

Turning on the debugging mode seemed to activate a Symfony custom exception handler that overrides error reporting.

Turning off the debugging mode seemed to bypass certain exceptions although my try-catch block is configured to catch general Exception objects. Really strange behavior.

Thanks!

elitalon
  • 9,191
  • 10
  • 50
  • 86
  • possible duplicate of [How to catch this error: "Notice: Undefined offset: 0"](http://stackoverflow.com/questions/5373780/how-to-catch-this-error-notice-undefined-offset-0) – DhruvPathak Oct 10 '11 at 12:30
  • possible duplicate of [Handling errors as exceptions. Best methods?](http://stackoverflow.com/questions/455331/handling-errors-as-exceptions-best-methods) – Piskvor left the building Oct 10 '11 at 12:39
  • I've updated the question, since I've already seen these solutions and did not solve my problem – elitalon Oct 10 '11 at 12:41
  • Can you verify why it didn't work? Is error_reporting turned off? Does the exceptions_error_handler every get called? – Paul Dixon Oct 10 '11 at 13:01
  • My fault. Symfony was being executed in production-mode. But now that I have turned it into debug-mode, Symfony seems to be implementing its own error handling mechanism that overrides my custom function. – elitalon Oct 10 '11 at 13:08

1 Answers1

3

See the answer to Handling errors as exceptions. Best methods? for a way to throw exceptions when an error is raised.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348