我正在使用带有许多try / catch块的PHP Exceptions,并且一切正常,但在一个特定的片段中除外。
请参阅以下代码:
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 (简化)
<?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;
}
}
当我运行代码和调用Merge构造函数时,即使$this->responses
为空,也会抛出异常,但它没有在Controller中捕获,我看到了通知
PHP注意:尝试在第96行的/var/www/ws-test/app/Controller/Controller.class.php中获取非对象的属性
指行return $merge->searchResponse;
当我调试代码时,我可以在throw $ex
中使用断点,但这不会被捕获。
难道我做错了什么?为什么忽略异常?
我在SO中看到了一些类似的问题,但任何描述同样的问题。
答案 0 :(得分:1)
代码中的某些内容不正确:
$this->searchResponse = array();
然后你返回一个空数组:
return $merge->searchResponse;
也许你的意思是:
return $merge->responses;
要确保捕获所有异常,请先捕获所有自定义异常,然后在最后一个catch块上添加Exception
:
try {
//code
} catch (AppException $ex){
$response = new RS_ERROR($this->client);
echo ($response);
}catch (Exception $e){
var_dump($e);
}