【发布时间】:2018-08-09 23:54:22
【问题描述】:
当我调用 $this->get() 并且它在 get 方法内抛出一个不是 ClientException 的异常时,最后一个 catch 块被调用,但是整个方法返回一个 NULL。
我已在最后一个 catch 块中使用 var_dump($e) 确认该错误正在被该 catch 块捕获...但 throw 似乎不起作用!
这让我大吃一惊,我不明白为什么。
public function get(string $url, $params = null): array
{
$client = new Client();
try {
$response = $client->get($url, ['query' => $params]);
return json_decode($response->getBody(), true);
} catch (ClientException $e) {
if (!empty($response = self::handleGuzzleErrors($e, $url))) {
return $response;
}
throw $e;
} catch (\Exception | \Throwable $e) {
var_dump($e);
throw $e;
}
}
public function getSub(string $url, array $subs, $params = null): array
{
$url = strtr($url, $subs);
$result = $this->get($url, $params);
return $result;
}
【问题讨论】: