0
我对许多try/catch块使用了PHP异常,并且所有工作都正常,除了一个特定的片段。PHP异常处理无法正常工作
见下面的码:
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;
}
}
当我运行代码和呼叫合并构造,即使$this->responses
为空时,Exception也会抛出,但它不是catche在控制器d,我看到通知
PHP的通知:试图让非对象的财产/var/www/ws-test/app/Controller/Controller.class.php上线96
指的是线路return $merge->searchResponse;
当我调试的代码,我可以使用throw $ex
断点,但这不是逮住。 我做错了什么?为什么这个例外被忽略?
我在这里看到一些类似的问题,但任何描述相同的问题。
如果添加'赶上(例外$前){}''的捕捞(AppException $ EX)后{'这是否捕获此异常? – RiggsFolly
@RiggsFolly。我还没有尝试过(mea culpa),但我现在更改为'Exception',然后错误被正确捕捉,尽管我不明白为什么 – James
因为该异常是由系统生成的,而不是手动生成的AppException。我猜想构造器中还有其他问题出错了,因此该对象并没有真正创建 – RiggsFolly