Home > Backend Development > PHP Tutorial > Yii cannot catch the exception solution

Yii cannot catch the exception solution

不言
Release: 2023-04-02 22:26:01
Original
2571 people have browsed it

This article mainly introduces the solution to the exception that Yii cannot catch. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Many students said that yii2 cannot catch it. Exceptions, after reading most of the problems, they are caused by incorrect understanding of catch. For example, the problem:

//a文件: 
function a() {
  throw new \yii\web\HttpException('我是数据库异常');
}

//b文件:
use yii\db\Exception;

try{
  a();
}
catch(Exception $e)
{
  echo "捕获到异常了";
}
Copy after login

However, there is no outputThe exception is caught, because catch The Exception of actually refers to catching the exception thrown by yii\db\Exception, and cannot catch the exception thrown by HttpException. All exceptions in

yii are inherited from Exception, so there are two rewriting methods.

Method 1:

catch(\yii\web\HttpException $e)
Copy after login

Method 2:

catch(\Exception $e)
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to PHP Chinese net!

Related recommendations:

How to use try_catch in yii2-wx

The above is the detailed content of Yii cannot catch the exception solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template