This article mainly introduces the problem of Yii can not catch the exception of the solution, has a certain reference value, now share to everyone, the need for friends can refer to
Many students say that yii2 catch the exception, see most of the problems, because of the understanding of the catch is incorrect, such as the problem:
A file: function A () { throw new \yii\web\httpexception (' I am database exception ');} B File: Use yii\db\exception;try{ A ();} catch (Exception $e) { echo "caught an exception";}
There is no output 捕获到异常了
, however, because catch
the Exception
actual meaning is to catch the yii\db\Exception
thrown exception and not catch the exception thrown by HttpException.
yii
All exceptions are inherited Exception
, so there are two ways to override them.
Method One:
catch (\yii\web\httpexception $e)
Method Two:
catch (\exception $e)
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!