Calling Exceptions in Models

In Vokuro, on resetting a password, an email is generated using

$this->getDI()
                ->getMail()
                ->send([
                    $this->user->email => $this->user->name,
                ], "Reset your password", 'reset', [
                    'resetUrl' => '/reset-password/' . $this->code . '/' . $this->user->email,
            ]);

in afterCreate method.

I am tring to catch exception by changing this like:

try{
            $this->getDI()
                ->getMail()
                ->send([
                    $this->user->email => $this->user->name,
                ], "Reset your password", 'reset', [
                    'resetUrl' => '/reset-password/' . $this->code . '/' . $this->user->email,
            ]);
        } catch (MailException $e) {
            $this->getDI()->getFlash()->error($e->getMessage());
        }

Am I in the right direction?

[UPDATE] On using \Swift_TransportException, the view is not rendering further and i get blank page. I changed back the above code to default and then in my controller action added try - catch like this:

$form = new ForgotPasswordForm();

        try{
            if ($this->request->isPost()) {
                // Send emails only is config value is set to true
                if ($this->getDI()->get('config')->useMail) {
                    if ($form->isValid($this->request->getPost()) == false) {
                        foreach ($form->getMessages() as $message) {
                            $this->flash->error((string) $message);
                        }
                    } else {
                        $user = Users::findFirstByEmail($this->request->getPost('email'));
                        if (!$user) {
                            $this->flash->warning('There is no account associated to this email');
                        } else {
                            $resetPassword          = new ResetPasswords();
                            $resetPassword->usersId = $user->id;
                            if ($resetPassword->save()) {
                                $this->flash->success('Success! Please check your messages for an email reset password');
                            } else {
                                foreach ($resetPassword->getMessages() as $message) {
                                    $this->flash->error((string) $message);
                                }
                            }
                        }
                    }
                } else {
                    $this->flash->warning(
                        'Service not available at the moment. Please try again later.'
                    );
                }
            }
        } catch (\Swift_TransportException $Ste) {
            $this->flash->error("Error.");
        }

        $this->view->setVar('form', $form);


2.5k

start with catch (\Exception $e)

You only need to wrap the part of the code that will be throwing the exception. Wrapping too much code makes it more difficult to find where the exception is actually being thrown.

If you specify a specific type of exception in a catch statement, then it only catches those exceptions. So in the code you've posted, if an exception other than Swift_TransportException is thrown, it won't be caught. I'm guessing that's what's happening here.

You have 2 approaches. You could do what @talal424 suggested and just catch the root Exception. All Exceptions extend the root Exception class, so if you just catch Exception, it will necessarily catch all Exceptions. The other approach is to have a separate catch() statement for each type of exception. This is useful if you want to have a different message or do different logic depending on which exception is thrown. Looking at your code and the generic feedback you're providing, I think just catching \Exception will probably be sufficient.

I'd also recommend enabling Phalcon's debug functionality at the top of your /public/index.php file. This generates some nice looking and helpful output which can help you narrow down where the problem is.

$Debug = new \Phalcon\Debug();
$Debug->listen();