PHP 异常处理无法正常工作
PHP exception handling doesn't work properly
我正在使用具有许多 try/catch 块的 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;
}
}
当我 运行 代码和调用 Merge 构造函数时,即使 $this->responses
为空,也会抛出异常,但它没有在控制器中捕获,我看到通知
PHP Notice: Trying to get property of non-object in /var/www/ws-test/app/Controller/Controller.class.php on line 96
指的是return $merge->searchResponse;
行
当我调试代码时,我可以在throw $ex
中使用一个断点,但是这个没有被捕获。
难道我做错了什么?为什么忽略异常?
我在 SO 中看到了一些类似的问题,但任何问题都描述了相同的问题。
您的代码中有些地方不正确:
$this->searchResponse = array();
然后你return一个空数组:
return $merge->searchResponse;
也许你的意思是:
return $merge->responses;
为确保捕获所有异常,首先捕获所有自定义异常,然后在最后一个捕获块中添加 Exception
:
try {
//code
} catch (AppException $ex){
$response = new RS_ERROR($this->client);
echo ($response);
}catch (Exception $e){
var_dump($e);
}
我正在使用具有许多 try/catch 块的 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;
}
}
当我 运行 代码和调用 Merge 构造函数时,即使 $this->responses
为空,也会抛出异常,但它没有在控制器中捕获,我看到通知
PHP Notice: Trying to get property of non-object in /var/www/ws-test/app/Controller/Controller.class.php on line 96
指的是return $merge->searchResponse;
当我调试代码时,我可以在throw $ex
中使用一个断点,但是这个没有被捕获。
难道我做错了什么?为什么忽略异常?
我在 SO 中看到了一些类似的问题,但任何问题都描述了相同的问题。
您的代码中有些地方不正确:
$this->searchResponse = array();
然后你return一个空数组:
return $merge->searchResponse;
也许你的意思是:
return $merge->responses;
为确保捕获所有异常,首先捕获所有自定义异常,然后在最后一个捕获块中添加 Exception
:
try {
//code
} catch (AppException $ex){
$response = new RS_ERROR($this->client);
echo ($response);
}catch (Exception $e){
var_dump($e);
}