Page 1 of 1

Zquery handle EDatabaseError

Posted: 22.04.2010, 19:01
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 ?

Posted: 23.04.2010, 12:52
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;

Posted: 23.04.2010, 19:15
by dewie
I can't find an ApplicationEvents Component in lazarus ?

Posted: 23.04.2010, 19:19
by PetyaMaster
Probably because it's Delphi specific ...
LOL :D

Posted: 24.04.2010, 15:55
by guidoaerts
Dewie,
you can assign any procedure that handles exceptions to the
application.onexception property

Guido