Home >PHP Framework >YII >How to catch yii2 exceptions

How to catch yii2 exceptions

angryTom
angryTomOriginal
2020-02-03 17:00:033096browse

How to catch yii2 exceptions

How to catch exceptions in yii2

All exceptions in yii are inherited from Exception. There are two ways to write exceptions

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

Writing method one:

try{
  a();
}
catch(\yii\web\HttpException $e)
{
  echo "捕获到异常了";
}

Writing method two:

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

Recommended related article tutorials: yii tutorial

The above is the detailed content of How to catch yii2 exceptions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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