HELPME
HELPME

Reputation: 754

Try/catch doesn't catch an exception in Yii2

as the title says, try/catch doesn't catch throwed exception.

Here is my code:

public function edit($id)
{
    try {
        $this->permissions($id);   
        return $this->redirect(['edit/list']);

    } catch (Exception $e) {
        Yii::$app->session->setFlash('error', Yii::t('app', 'PERMISSIONS_NOT_FOUND'));
    }   
}

I'm trying to catch an exception which is in permissions() method. Permissions method is:

private function persimssions($id)
{
    $permitted = false;

    if (!$permitted) {
        throw new ForbiddenHttpException(Yii::t('app', 'MODULE_NO_PERMISSIONS'));
    }
}

Could someone explain me what is wrong? Thanks for any help

Upvotes: 1

Views: 1585

Answers (1)

Bizley
Bizley

Reputation: 18021

I almost sure that your Exception is in fact yii\db\Exception - check this in use statements. If so it's obvious that it will not catch ForbiddenHttpException.

If you want to catch all exceptions write \Exception in catch or add proper use statement.

Upvotes: 6

Related Questions