Welcome Guest, Not a member yet? Register   Sign In
Catch exeption from database insert
#1
Exclamation 

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?
Reply
#2

Try \Throwable $e
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 06-28-2024, 01:20 AM by Ñuño Martínez.)

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.
Reply
#4

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

(This post was last modified: 06-28-2024, 02:26 AM by Ñuño Martínez.)

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

(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
Reply
#7

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

(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.
Reply
#9

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB