Page 1 of 1

Catch connection error on programm start

Posted: Mon 22 Jul 2019 08:36
by Conny
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:

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;
Then I did some research and found that I should use the OnError event.
So I added the line:

Code: Select all

FDatabaseConnection.OnError := HandleDbError;
directly after the line

Code: Select all

FDatabaseConnection := TUniConnection.Create(MainForm);
and the procedure:

Code: Select all

procedure TAppData.HandleDbError(Sender: TObject; E: EDAError;
  var Fail: Boolean);
begin
  Fail := False;
end;
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.

Re: Catch connection error on programm start

Posted: Mon 22 Jul 2019 11:05
by ViktorV
This UniDAC behavior is correct and described in the UniDAC documentation: you get EAbort Exception: "Operation aborted" because you set the Fail parameter to False: https://www.devart.com/unidac/docs/deva ... error. htm
To solve your problem, you can use the following code:

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 E: EUniError do begin
      FDatabaseError := True;
      FCriticalProgramError := True;
       raise Exception.Create('UniError. Message - ' + E.Message)
    end
    else
      raise;
  end;

Re: Catch connection error on programm start

Posted: Tue 23 Jul 2019 05:48
by Conny
Hi ViktorV,

thank you very much for your answer. Now I get why I get the EAbort Exception. I did not find the link.

However, using the provided code does not solve my problem. I placed a stop point for the debugger in the first line of the except clause. Then I started the debugger. Several error messages occur and I have to press the button that makes the program continue and after several errors, the debugger stopps in the except clause.

When I run the program in the normal mode, it still displays the error message via the message dialog. Only the text that is displayed is different. That's not what I want to achieve.

Maybe I should explain my problem a little bit different. Imagine a form which has only the purpose to display a green text if the connection to the database could be established or a red text if the connection could not be established. In my case, instead of starting and showing the red text, the program shows the error message dialog and immediately closes itself.

Re: Catch connection error on programm start

Posted: Tue 23 Jul 2019 06:49
by ViktorV
To prevent the TUniError error message window from being displayed, you can remove the

Code: Select all

raise Exception.Create ('UniError. Message -' + E.Message); 
line in the example above.

Re: Catch connection error on programm start

Posted: Tue 23 Jul 2019 09:59
by Conny
Hi ViktorV,

thank you for your answer!
I was totally confused, because your resolution was basically the same as I did from the beginning. So I wrote a test program in in that programm, everything worked.
Then I rechecked my big program and found out, that I had a Query in in where I did not check if the database connection was set up successfully. After fixing it, it worked.

It was very confusing, that the debug mode did not stop at that point but instead still at the point where I tried to create the datbase connection in the first place.

Re: Catch connection error on programm start

Posted: Tue 23 Jul 2019 11:19
by ViktorV
Thank you for the interest to our product.
It is good to see that the issue has been solved.
If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.