Page 1 of 1

PostgreSQL exception handling problem

Posted: Mon 24 Apr 2017 00:21
by ertank
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.

Re: PostgreSQL exception handling problem

Posted: Mon 24 Apr 2017 11:55
by AlexP
Hello,

You should use the UniConnection.OnError event

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

Re: PostgreSQL exception handling problem

Posted: Mon 24 Apr 2017 18:25
by ertank
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.

Re: PostgreSQL exception handling problem

Posted: Tue 25 Apr 2017 06:31
by AlexP
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
...

Re: PostgreSQL exception handling problem

Posted: Tue 25 Apr 2017 08:38
by ertank
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.

Re: PostgreSQL exception handling problem

Posted: Wed 24 May 2017 09:52
by AlexP
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

Re: PostgreSQL exception handling problem

Posted: Wed 29 Apr 2020 17:33
by donaldshimoda
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

Re: PostgreSQL exception handling problem

Posted: Wed 29 Apr 2020 17:48
by donaldshimoda
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;