【发布时间】:2017-04-07 17:43:37
【问题描述】:
我有一个自定义类:
class ActivationService extends SmsException {
public function __construct()
{
$this->sms = new SmsSender();
}
public function method(){
throw new SmsException(); // My custom exception
}
public function send(){
$this->sms->sendSms($this->phone); // Here's where the error appeared
}
}
所以,当我调用 $this->sms->sendSms 时,我收到了来自类 sms 的错误。
我正在捕捉自定义异常,例如:
try {
$activationService = new ActivationService();
$activationService->send($request->phone);
}
catch (SmsException $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
但是当我在方法库 (class SmsSender) 中收到错误时:send() 我无法捕获它并收到错误。
我该如何解决?
【问题讨论】:
-
它可能没有抛出 SmsException。为
\Exception添加另一个 catch 块,看看它是否捕获了一些东西。 -
是的,
\Exception有效,但是为什么我的异常不起作用:use Exception; class SmsException extends Exception { // TODO } -
你需要
\SmsException吗?我知道命名空间可以解决这个问题。
标签: php laravel exception laravel-5.3