How to manage exceptions ?

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

How to manage exceptions ?

Post by yapt » Fri 25 Jun 2010 15:53

Hello,

I am trying to manage specific exceptions on PgDAC.

In example, I am trying to do a post on a table but a field must not be null (a primary key for example):

Code: Select all

try
  myPgTable.Post;
except
  on E: EPgError do
  begin
     ShowMessage('it is a PgDAC Exception');
  end;
  on E: EDAError do
  begin
     ShowMessage('it is a .... Database Exception on the server side ?');
  end;
  on E: Exception do
  begin
     ShowMessage('it is a generic exception');
  end;
end;
But the exception is a generic EDatabaseError exception. With a very limited information inside.

How I must to "manage" PgDAC exception ? I have been looking for something in the documentation but I cannot find anything usefull.

Thanks.

bork
Devart Team
Posts: 649
Joined: Fri 12 Mar 2010 07:55

Post by bork » Tue 29 Jun 2010 11:14

Hello

To handle PgDAC exceptions you can use the following code:

Code: Select all

    on E: EPgError do
    begin
       ShowMessage('It is a PgDAC Exception: ' + E.ErrorCode + ' - ' + E.Message);
    end;
To implement this code you should add the PgError unit to the USES section of your unit.

More detailed information about EPgError you can find in the Reference -> PgError section of the UniDAC help.

yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

Post by yapt » Tue 29 Jun 2010 13:08

Hello Bork,

if you see my example code on my first post, the message showed is:

Code: Select all

'it is a generic exception'
So it is NOT a EPgError. As I said, it is a 'EDatabaseError'. Perhaps I am doing something wrong but it is very simple.

Regards.

yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

Post by yapt » Wed 30 Jun 2010 10:37

Any advice on this ?

Thanks...

bork
Devart Team
Posts: 649
Joined: Fri 12 Mar 2010 07:55

Post by bork » Wed 30 Jun 2010 14:02

Hello

EDatabaseError has the Message property. EDAError has the ErrorCode property and the Message property. You can manage your exceptions by the following code:

Code: Select all

  try
    ...;
  except
    on E: EPgError do
       ShowMessage('It is EPgError: ' + E.ErrorCode + ' - ' + E.Message);
    on E: EDAError do
       ShowMessage('It is EDAError: ' + IntToStr(E.ErrorCode) + ' - ' + E.Message);
    on E: EDatabaseError do
       ShowMessage('It is EDatabaseError: ' + E.Message);
    on E: Exception do
       ShowMessage(E.Message);
  end;
If you want to manage your exceptions with other results please give a detailed description of it and I will try to help you.

yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

Post by yapt » Wed 30 Jun 2010 15:18

Hello Bork,

perhaps I have not explain myselft right. My problem is a PgTable.Post go inside EDatabaseError exception manager but NOT EpgError nor EDAError and I would like to get the "extended" information on a EPgError exception manager.

On your example:

Code: Select all

  try 
   [b] MyPGTable.Post;[/b]
  except 
    on E: EPgError do 
       ShowMessage('It is EPgError: ' + E.ErrorCode + ' - ' + E.Message); 
    on E: EDAError do 
       ShowMessage('It is EDAError: ' + IntToStr(E.ErrorCode) + ' - ' + E.Message); 
    on E: EDatabaseError do 
       ShowMessage('It is EDatabaseError: ' + E.Message); 
    on E: Exception do 
       ShowMessage(E.Message); 
  end;
I see this message (ShowMessage) when I execute the application and try to post to myPgTable with a field with NULL value (the field is a NOT NULL field):
'It is EDatabaseError: Field 'xxxxxxx' must have a value'
Why I cannot manage the error with EPGError ?

I hope to explain my self a bit better.

Kind regards.

bork
Devart Team
Posts: 649
Joined: Fri 12 Mar 2010 07:55

Post by bork » Thu 01 Jul 2010 10:10

Hello

The exception class depends on the place where error was generated. If error was generated by database then you will get EPgError, but if error was generated by the PgDAC components then you will get EDAError or EDatabaseError.

In your case TPgQuery or TPgTable has the Options.RequiredFields options that determine where error for required fields will be generated. If you set the value of this option to False then you will get EPgError. For standard update you cannot find difference in the behavior. But if you try to update database in the cached update mode then if this option is set to True then error for required fields will be generated on calling the Post method, but if this option is set to False then PgDAC will not check required fields and this error will be generated on storing data to database (on calling the ApplyUpdates method).

yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

Post by yapt » Thu 01 Jul 2010 13:24

Hi Bork, I appreciate your explanation but ....
The exception class depends on the place where error was generated. If error was generated by database then you will get EPgError, but if error was generated by the PgDAC components then you will get EDAError or EDatabaseError.
How can I know WHERE every exception is generated without testing everyone ?
In your case TPgQuery or TPgTable has the Options.RequiredFields....
My help for PgDataSet.options:
ms-help://embarcadero.rs2010/Devart.PgDac/PgDac/Devart.PgDac.TCustomPgDataSet.Options.htm

Has _only_ 9 options in spite of my IDE has 31 property options available. I have an old help file installed I think because I have had problems with the whole Delphi help file system. Where I can get any up to date help file and install it manually ?

Thanks...

bork
Devart Team
Posts: 649
Joined: Fri 12 Mar 2010 07:55

Post by bork » Mon 05 Jul 2010 12:21

Hello
How can I know WHERE every exception is generated without testing everyone ?
You set the Options.RequiredFields property, so you determine where exception will be generated.
My help for PgDataSet.options:
ms-help://embarcadero.rs2010/Devart.PgDac/PgDac/Devart.PgDac.TCustomPgDataSet.Options.htm

Has _only_ 9 options in spite of my IDE has 31 property options available. I have an old help file installed I think because I have had problems with the whole Delphi help file system. Where I can get any up to date help file and install it manually ?
The TPgDataSetOptions class is inherited from the TDADataSetOptions calss. You can open your help: ms-help://embarcadero.rs2010/Devart.PgDac/PgDac/Devart.Dac.TDADataSetOptions%20members.htm and see the RequiredFields option description.

yapt
Posts: 69
Joined: Mon 16 Mar 2009 12:06

Post by yapt » Mon 05 Jul 2010 14:53

Thanks a lot Bork.

Best regards.

Post Reply