PostgreSQL exception handling problem

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

PostgreSQL exception handling problem

Post by ertank » Mon 24 Apr 2017 00:21

Hello,

Using UniDAC 7.0.1 on Delphi 10.2. Target is 32bit executable. Database is PostgreSQL 9.6.2 64bit (EDB Binaries used for installing) running on Windows 10 64bit OS.

Using try..except block to catch any error on database commands is not working. If any exception raises, it is simply, directly displayed on the screen than being catched in my except block. In other words, except blocks are never executed.

Sample application to reproduce the problem can be downloaded from here: postgresql_exception_handling.rar

If this is a feature, please advise how can I use UniDAC the right way to catch these exceptions.

Thanks & regards.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: PostgreSQL exception handling problem

Post by AlexP » Mon 24 Apr 2017 11:55

Hello,

You should use the UniConnection.OnError event

and to handle the exception set the Fail parameter to False;

ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

Re: PostgreSQL exception handling problem

Post by ertank » Mon 24 Apr 2017 18:25

Hello,

Below code helped to solve my problem.

Code: Select all

procedure TDM.DBError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
  Fail := False;
end;
TDM is a TDataModule
DB is name of TUniConnection

Normally, I did not use that TUniConnection.OnError() procedure when I am working with SQL Server or SQLite. Actually, this is the first time I am using it. Is this way of error handling specific to PostgreSQL?

Thanks.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: PostgreSQL exception handling problem

Post by AlexP » Tue 25 Apr 2017 06:31

For this, you can check the exception class

Code: Select all

uses ..., PgErrorUni;

...

procedure TForm1.UniConnection1Error(Sender: TObject; E: EDAError;
  var Fail: Boolean);
begin
  if E is EPgError then
...

ertank
Posts: 172
Joined: Wed 13 Jan 2016 16:00

Re: PostgreSQL exception handling problem

Post by ertank » Tue 25 Apr 2017 08:38

Dear Alex,

I just realize that suggested usage is necessary for TUniScript exception handling. TUniQuery default executing except block without any code written for TUniConnection.OnError() procedure.

Thanks for the help.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: PostgreSQL exception handling problem

Post by AlexP » Wed 24 May 2017 09:52

This different behavior is implemented because UniScript can execute multiple commands in a row, and even if there is an error in one of the commands, it would be possible to continue execution of other commands. If you don't want to use the global OnError event in UniConnection you can use the OnError event for each UniScript

donaldshimoda
Posts: 13
Joined: Thu 16 Aug 2018 15:54

Re: PostgreSQL exception handling problem

Post by donaldshimoda » Wed 29 Apr 2020 17:33

Sorry but no, is not clear. And is broken in the latest version

I want to ignore scripts error when active a variable UniScriptIgnoreErrors. How to do?

Cant find a correct combination for that

my code is inside a try except block.

If I use this solution, your library manages the exception showing a visual error. That not happens in previous versions.Why?

Code: Select all

procedure TdmBaseLocal.UniScript1Error(Sender: TObject; E: Exception; SQL: string; var Action: TErrorAction);
begin
  if UniScriptIgnoreErrors then
  begin
    Action  := eaContinue;
  end
  else
  begin
    Action := eaException;
  end;
end;
If I use the following solution Never get UniScript1Error so I can't control when it raise errors.

Code: Select all

procedure TdmBaseLocal.UniScript1Error(Sender: TObject; E: Exception; SQL: string; var Action: TErrorAction);
begin
  if UniScriptIgnoreErrors then
  begin
    Action  := eaContinue;
  end
  else
  begin
    Action := eaException;
  end;
end;

procedure TdmBaseLocal.UniConnectionError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
  Fail := False;
end;
Please explain step by step how to be able to manage in a try exception block an unidac exception and how to make able to ignore that , as was possible in the past.

P.S. im using version 8.1.3 in Delphi RIO

donaldshimoda
Posts: 13
Joined: Thu 16 Aug 2018 15:54

Re: PostgreSQL exception handling problem

Post by donaldshimoda » Wed 29 Apr 2020 17:48

I find the correct code to do that work as work in the past

Code: Select all

procedure TdmBaseLocal.UniConnectionError(Sender: TObject; E: EDAError; var Fail: Boolean);
begin
  Fail := True;
end;

procedure TdmBaseLocal.UniScript1Error(Sender: TObject; E: Exception; SQL: string; var Action: TErrorAction);
begin
  if UniScriptIgnoreErrors then
  begin
    Action  := eaContinue;
  end
  else
  begin
    Action := eaFail;
  end;
end;

Post Reply