CodeIgniter Forums
Catch exeption from database insert - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Catch exeption from database insert (/showthread.php?tid=91174)

Pages: 1 2


Catch exeption from database insert - Ñuño Martínez - 06-27-2024

Developing a web application, I'm using insert () to add registers to the database.  I'm using the optional parameter to return a boolean value so I can check if it succeeded.

The problem is that if I try to insert a new register with a yet existing key CodeIgniter catch the exception and shows the tracing information.  So I added a try..catch but it doesn't catch anything.

The code I use is:

PHP Code:
try
{
  $Result $this->insert ($Datafalse);
}
catch (\
Exception $Error)
{
  log_message ('error'$Error->getMessage ());
  $Result false;


I've tried several exception classes (including CodeIgniter\Database\Exception\DatabaseException) but nothing works, neither debug mode nor production mode: CI overrides my code.  finally does work though.

Why is CI overriding my try..catch blocks and how can I prevent it?

P.S: I've search both in forums and in the Internet but I've found only old threads (CI3 or CI2) or unrelated with my problem.

P.S.2: If you'll say that I should check data before insert (as I've read in some old threads), I shouldn't:  I want the database to check data integrity and just check if it succeeded.  That's why relational databases were invented for, aren't they?


RE: Catch exeption from database insert - ozornick - 06-27-2024

Try \Throwable $e


RE: Catch exeption from database insert - Ñuño Martínez - 06-28-2024

It works. Thank you.

I'm still not used to the new* Trowable hierarchy. I still think the base class is Exception.
_________________________

* I know it's not new-new.


RE: Catch exeption from database insert - kenjis - 06-28-2024

In my opinion, it should be DatabaseException.
What database driver do you use?


RE: Catch exeption from database insert - Ñuño Martínez - 06-28-2024

MySQLi, but the idea is that the customer can use any one available.


RE: Catch exeption from database insert - Bosborne - 06-29-2024

(06-28-2024, 01:20 AM)Ñuño Martínez Wrote: It works.  Thank you.

I'm still not used to the new* Trowable hierarchy.  I still think the base class is Exception.
_________________________

* I know it's not new-new.

Throwable is more general than Exception in PHP. It has been around since PHP 7, so it is not new.

Throwable is the base interface for any object that can be thrown via a throw statement, including Error and Exception.

from https://www.php.net/manual/en/class.throwable.php


RE: Catch exeption from database insert - kenjis - 06-29-2024

And catching large class like Throwable is not good practice.
Because it catches all exceptions including others than you want to catch.


RE: Catch exeption from database insert - Ñuño Martínez - 07-09-2024

(06-29-2024, 04:27 PM)kenjis Wrote: And catching large class like Throwable is not good practice.
Because it catches all exceptions including others than you want to catch.

I know, but I don't know what other exception can I catch. As I've said I tried to catch a DataBase exception without success.


RE: Catch exeption from database insert - InsiteFX - 07-09-2024

Code:
This may shed some light on the subject.

Add This one line:
mysqli_report(MYSQLI_REPORT_ALL);

StackOverflow - Catch does not report an error when mysqli INSERT fails.


RE: Catch exeption from database insert - Ñuño Martínez - 07-15-2024

(07-09-2024, 09:58 PM)InsiteFX Wrote:
Code:
This may shed some light on the subject.

Add This one line:
mysqli_report(MYSQLI_REPORT_ALL);

StackOverflow - Catch does not report an error when mysqli INSERT fails.

Thank you.  That's what I was looking foor but I didn't found.