dsgm5631
2017-11-06 09:06
浏览 171
已采纳

Try / catch在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

图片转代码服务由CSDN问答提供 功能建议

正如标题所示, try / catch 不会捕获抛出异常。

这是我的代码:

  public function edit($ id)
 {
 try {
 $ this-> permissions  ($ ID);  
返回$ this-> redirect(['edit / list']); 
 
} catch(Exception $ e){
 Yii :: $ app-> session-> setFlash('error'  ,Yii :: t('app','PERMISSIONS_NOT_FOUND')); 
} 
} 
   
 
 

我正在尝试捕获一个异常 permissions()方法。 权限方法是:

 私有函数persimssions($ id)
 {
 $ allowed = false; 
 
 if if(!$ allowed){
 throw new  ForbiddenHttpException(Yii :: t('app','MODULE_NO_PERMISSIONS')); 
} 
} 
   
 
 

有人可以解释我的错误吗? 感谢您的帮助

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpao5658 2017-11-06 09:17
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题