Home > Article > Backend Development > Yii cannot catch the exception solution
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 "捕获到异常了"; }
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)
Method 2:
catch(\Exception $e)
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!