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!

5
  • possible duplicate of How to catch this error: "Notice: Undefined offset: 0" Commented Oct 10, 2011 at 12:30
  • possible duplicate of Handling errors as exceptions. Best methods? Commented Oct 10, 2011 at 12:39
  • I've updated the question, since I've already seen these solutions and did not solve my problem Commented Oct 10, 2011 at 12:41
  • Can you verify why it didn't work? Is error_reporting turned off? Does the exceptions_error_handler every get called? Commented Oct 10, 2011 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. Commented Oct 10, 2011 at 13:08

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.