PHP exception handling doesn't work properly -
i'm using php exceptions many try/catch blocks , works fine, except in 1 specific snippet.
see 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 run code , call merge constructor, when $this->responses
empty, exception thrown, not catched in controller , see notice
php notice: trying property of non-object in /var/www/ws-test/app/controller/controller.class.php on line 96
refers line return $merge->searchresponse;
when debug code, can use breakpoint in throw $ex
, not catched. doing wrong? why exception ignored?
i see similar questions here in so, describe same problem.
something not correct in code:
$this->searchresponse = array();
then return empty array:
return $merge->searchresponse;
perhaps meant:
return $merge->responses;
to make sure catch exception, catch custom ones first add exception
on last catch block:
try { //code } catch (appexception $ex){ $response = new rs_error($this->client); echo ($response); }catch (exception $e){ var_dump($e); }
Comments
Post a Comment