Zquery handle EDatabaseError

In this forum we will discuss things relating the ZEOSLib 6.6.x stable versions

Moderators: gto, EgonHugeist

Post Reply
dewie
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 22.04.2010, 18:41

Zquery handle EDatabaseError

Post by dewie »

Stupid question i think, but how can i handle a EdatabaseError
I have a very simple application that connects to a postgresql database with a zquery and a dbgrid. The postgresql database has a lot of validations, and for know i just want to handle the error by showing a nice alert.

Where to handle the EdatabaseError ?
PetyaMaster
Fresh Boarder
Fresh Boarder
Posts: 19
Joined: 22.04.2010, 13:09

Post by PetyaMaster »

If you use statically created components on your forms, put an ApplicationEvents component on the main form. It has an OnException event handler. Implement this handler to catch all exceptions that are not handled otherwise, and would cause your app to terminate unless Delphi's default handler displays that annoying message with the details.

But best practice is not to forget to encapsulate critical sections into try ... except blocks. Neither too much, nor too few blocks are good, but the latter is more risky. The only question is what are critical sections, but now I don't wanna dig into that topic any deeper.


Another way of approach that I'm doing mostly is creating the ZQuery components in runtime, someway like this :

Code: Select all

procedure MyProc;
var QueryText: string;
begin
  ...
  try with TZQuery.Create(Form1) do try
    Connection:=Form1.ZConnection;
    SQL.Add('This is my Query');
    Open;
    ...
  finally
    QueryText:=SQL.Text;
    Free;
  end;
  except
    on E: Exception do
      FileLog(DatabaseErrors,QueryText + E.Message); //My own method
  end;
end;
dewie
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 22.04.2010, 18:41

Post by dewie »

I can't find an ApplicationEvents Component in lazarus ?
PetyaMaster
Fresh Boarder
Fresh Boarder
Posts: 19
Joined: 22.04.2010, 13:09

Post by PetyaMaster »

Probably because it's Delphi specific ...
LOL :D
guidoaerts
Senior Boarder
Senior Boarder
Posts: 93
Joined: 01.07.2009, 16:07

Post by guidoaerts »

Dewie,
you can assign any procedure that handles exceptions to the
application.onexception property

Guido
Post Reply