4
我有一個自定義類:如何從另一個類捕獲異常?
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; 類SmsException擴展異常 { // TODO }' – Goga
您是否需要'\ SmsException'?我知道命名空間可能會擾亂這一點。 – nerdlyist