开发者

Is it possible to catch errors that are not exceptions in Symfony?

开发者 https://www.devze.com 2023-04-12 03:35 出处:网络
I have a try-catch block that iterates over a set of records like this: try { foreach ( $json[\'location\'] as $records ) {

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->se开发者_如何学CtName($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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消