CodeIgniter Forums
Try catch is not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Try catch is not working (/showthread.php?tid=91793)



Try catch is not working - rav1971 - 10-12-2024

Im trying to catch errors when data is not inserted on the database.
This is may code:
try {
$id = (new CatalogModel)->create([
'role' => post('role'),
'title' => post('title'),
]);
model('App\Models\ActivityLogModel')->add('Se ha creado un nuevo grupo: '.$id.' Creado por: '.logged('name'), logged('id'));
} catch (\Exception $e) {
model('App\Models\ActivityLogModel')->add('Error al crear grupos:'. $e->errorMessage(), logged('id'));
}

But its not working and allways redirect to error page (errors/html/production.php).

Any help on this?


RE: Try catch is not working - sikander - 10-15-2024

There is no add() method in the Model class. If you are trying to insert a row use insert() or update() or save() if updating existing records.