私は、カスタムクラスを持っている:だから他のクラスから例外をキャッチする方法は?
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'のために別のキャッチブロックを追加し、それが何かをキャッチするかどうかを確認してください。 –
はい、 '\ Exception'は動作しますが、なぜ私の例外が動作しないのですか? クラスSmsException例外を拡張します { // TODO } ' – Goga
' \ SmsException'が必要ですか?私は名前空間がこれをぶつけることができることを知っています。 – nerdlyist