Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Yii2 is down with try/catch?

Tags:

php

yii2

I insert an entry in which there is a duplicate of the primary key.

public function actionInc()
{
        $add = new Country();
        $add->code = 'QI';
        $add->name = 'Qiang';
        $add->population = '4444444';
    try {
        $add->save();
        return $this->render('inc', [
            'count' => 'Ok',
        ]);
    } catch (Exception $exception) {
        return $this->render('inc', [
            'count' => 'Error',
        ]);
    }
}

But I need that the application does not down, and continued to work, but it does not work... screenshot

like image 969
fosh4455 Avatar asked Jan 31 '23 00:01

fosh4455


1 Answers

check which Exception subclass you are importing in your use statements
yii throws \yii\db\Exception for db-related errors.
all of yii's exceptions inherit from \Exception

// db related exceptions
catch (\yii\db\Exception $exception) 

// any exception throwin by yii
catch (\yii\base\Exception $exception)

// any php exception
catch (\Exception $exception)
like image 188
csminb Avatar answered Feb 03 '23 06:02

csminb