I'm using the PHP Exceptions with many try/catch blocks and all works fine, except in one specific snippet.
See the codes below:
Controller.class
<?php namespace Controller; use Business\Exceptions\AppException; use Business\OTA\Responses\Erros\RS_ERROR; use Utils\Merge; class Controller{ //other methods public main(){ //do stuff $resp = $this->merge($con) } private function merge($con) { try { $merge = new Merge($this->record, $con); $merge->sortResponses(); return $merge->searchResponse; } catch (AppException $ex){ $response = new RS_ERROR($this->client); echo ($response); } } } Merge.class (simplified)
<?php namespace Utils; use Business\Exceptions\AppException; use Exception; class Merge { public $responses; public $conectors; public $searchResponse; /** * Method __construct * @param array $conectorResponses * @param $conectors * @throws \Business\Exceptions\AppException */ public function __construct(array $conectorResponses, $conectors) { $this->responses = $conectorResponses; $this->conectors = $conectors; $this->searchResponse = array(); if (empty($this->responses)) { $ex = new AppException("Search Not found", '11'); throw $ex; } } When I run the code and the call Merge constructor, even when $this->responses is empty, the Exception is thrown, but it is not catched in the Controller and I see the notice
PHP Notice: Trying to get property of non-object in /var/www/ws-test/app/Controller/Controller.class.php on line 96
Refers to the line return $merge->searchResponse;
When I debug the code, I can use a breakpoint in throw $ex, but this is not catched. Am I doing something wrong? Why the exception is ignored?
I see some similar questions here in SO, but any describe the same problem.
catch (Exception $ex) {}after thecatch (AppException $ex){does that catch this exception?Exceptionright now and then the error is correctly catched, although I do not understand why