Catch connection error on programm start
Posted: Mon 22 Jul 2019 08:36
Hello (:
I have the following Problem:
When my Delphi program starts, it tries to create a database connection. If e.g. the provided password is wrong or the db server is unreachable, I want to handle that error in my program by showing a form with some text.
Currently, the program exits directly and shows a dialog containing the error and I'm not able to prevent it.
My first approach was:
Then I did some research and found that I should use the OnError event.
So I added the line:
directly after the line
and the procedure:
The latter leads to an EAbort Exception: "Operation aborted".
What am I doing wrong? How can I catch the error?
In general, I want to catch if the database connection was lost during program execution and show a custom message.
I have the following Problem:
When my Delphi program starts, it tries to create a database connection. If e.g. the provided password is wrong or the db server is unreachable, I want to handle that error in my program by showing a form with some text.
Currently, the program exits directly and shows a dialog containing the error and I'm not able to prevent it.
My first approach was:
Code: Select all
try
FDatabaseConnection := TUniConnection.Create(MainForm);
FDatabaseConnection.ProviderName := 'MySQL';
FDatabaseConnection.Username := FUser;
FDatabaseConnection.Password := FPassword;
FDatabaseConnection.Server := FServer;
FDatabaseConnection.Port := FPort;
FDatabaseConnection.Database := FDatabase;
FDatabaseConnection.Open;
except
on EUniError do
begin
FDatabaseError := True;
FCriticalProgramError := True;
end;
end;
So I added the line:
Code: Select all
FDatabaseConnection.OnError := HandleDbError;
Code: Select all
FDatabaseConnection := TUniConnection.Create(MainForm);
Code: Select all
procedure TAppData.HandleDbError(Sender: TObject; E: EDAError;
var Fail: Boolean);
begin
Fail := False;
end;
What am I doing wrong? How can I catch the error?
In general, I want to catch if the database connection was lost during program execution and show a custom message.