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 ?
Zquery handle EDatabaseError
Moderators: gto, EgonHugeist
-
- Fresh Boarder
- Posts: 19
- Joined: 22.04.2010, 13:09
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 :
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;
-
- Fresh Boarder
- Posts: 19
- Joined: 22.04.2010, 13:09
-
- Senior Boarder
- Posts: 93
- Joined: 01.07.2009, 16:07