fosh4455
fosh4455

Reputation: 371

Why Yii2 is down with try/catch?

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

Upvotes: 4

Views: 10161

Answers (1)

csminb
csminb

Reputation: 2382

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)

Upvotes: 8

Related Questions