Recent

Author Topic: Unable to use ESQLDatabaseError  (Read 4238 times)

talorigomat

  • Jr. Member
  • **
  • Posts: 96
Unable to use ESQLDatabaseError
« on: October 13, 2017, 06:28:03 pm »
I'm trying to use ESQLDatabaseError to catch the error code and message from a Firebird database but am having no joy.  I'm using the following code:
Code: Pascal  [Select][+][-]
  1.     try
  2.       dmMain.qrySite.ApplyUpdates();
  3.       dmMain.trnMain.Commit;
  4.  
  5.     except
  6.       on E:ESQLDatabaseError do
  7.       begin
  8.         ShowMessageFmt('ErrorCode: %d; Message: %s', [E.ErrorCode, E.Message]);
  9.       end;

if I use DatabaseError exception as

Code: Pascal  [Select][+][-]
  1.     try
  2.       dmMain.qrySite.ApplyUpdates();
  3.       dmMain.trnMain.Commit;
  4.       on E:EDatabaseError  do
  5.       begin
  6.         ShowMessage(E.Message);
  7.       end;
  8.     end;  

Then I can catch the exceptions, however this form doesn't give me the databases sql error code.

I'm using the SQLDB components to access the database and I'm using Lazarus 1.8.0 RC4 with MS Windows 10.
Lazarus 1.8.0Rc4, Windows 10

balazsszekely

  • Guest
Re: Unable to use ESQLDatabaseError
« Reply #1 on: October 14, 2017, 07:16:22 am »
What is the actual exception in your second code?

talorigomat

  • Jr. Member
  • **
  • Posts: 96
Re: Unable to use ESQLDatabaseError
« Reply #2 on: October 17, 2017, 03:59:58 pm »
Apologies.  Been away for a bit.  It is Violation of PRIMARY or UNIQUE KEY constraint "UK_STENME" ON TABLE "SITE"
Lazarus 1.8.0Rc4, Windows 10

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Unable to use ESQLDatabaseError
« Reply #3 on: October 17, 2017, 04:06:11 pm »
Your second code is invalid because "on E: Exception" always needs to be in an except-block.

Try this and tell us what classname you get.
Code: Pascal  [Select][+][-]
  1. try
  2.   dmMain.qrySite.ApplyUpdates();
  3.   dmMain.trnMain.Commit;
  4. except
  5.   on E: Exception do
  6.     Showmessage('Exceptionclass = ' + E.ClassName + ' / ' + E.Message);
  7. end;

You can use the text of ClassName as the actual exceptionclass.
And if that class is really an SQLException you can use E.ErrorCode after you change "on E: Exception" to "on E: Excptionclassname".


talorigomat

  • Jr. Member
  • **
  • Posts: 96
Re: Unable to use ESQLDatabaseError
« Reply #4 on: October 17, 2017, 05:40:37 pm »
Hi Rvk

thanks for pointing that out.  I accidentally deleted that before posting.  Using the code you supplied the class name returned is "EUpdateError".
Lazarus 1.8.0Rc4, Windows 10

talorigomat

  • Jr. Member
  • **
  • Posts: 96
Re: Unable to use ESQLDatabaseError
« Reply #5 on: October 17, 2017, 07:05:18 pm »
I've attached a simple project.  The place of interest is on the btnSaveClick method on the Site form.  To illustrate I've included  on E:ESQLDatabaseError, on E:EDatabaseError and on E: Exception in the exception block.

Commenting out E:EDatabaseError and E: Exception blocks exceptions go through to the default exception handler bypassing the ESQLDatabaseError handler.  uncomment either E:EDatabaseError and E: Exception then the exception is handled by the one that is uncommented.  ESQLDatabaseError isn't catching anything.
Lazarus 1.8.0Rc4, Windows 10

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Unable to use ESQLDatabaseError
« Reply #6 on: October 17, 2017, 07:11:57 pm »
Using the code you supplied the class name returned is "EUpdateError".
So... don't use ESQLDatabaseError but use EUpdateError.

It also has a ErrorCode property.

ESQLDatabaseError isn't catching anything because that's not the exception that occurs.

talorigomat

  • Jr. Member
  • **
  • Posts: 96
[Solved]: Unable to use ESQLDatabaseError
« Reply #7 on: October 19, 2017, 06:32:35 pm »
Thanks for the helping with this rvk.  Now I understand this better.
Lazarus 1.8.0Rc4, Windows 10

 

TinyPortal © 2005-2018