How to handle error generated while sending mail by swift mail

I am using Swiftmail to send SMTP mails .

Some time because of Invalid email or because of email server send limit I am getting error from swift mail. Is their any way to capture those errors and display a custom error message eg:- Problem in sending email, resend email.

Is there any way of using try(){}catch();or handling that exception so that user will not get the red page with errors . I had already set my application mode to production but I am still getting error messages for sent email s as response from email server .


This is what I used as solution

use \yii\base\ErrorException;

try {

Swift Mail Code

 }catch(\Swift_TransportException $exception) {

       Exception code