Page 1 of 1

how to handle exception if MySQL server does not respond ?

Posted: 16.07.2008, 20:09
by thcom
hi, i try this code:

function ConnectMySQL(iConexe: TZConnection; iIP: String; iDB : String): Boolean;
begin
Screen.Cursor := crSQLWait;
ConnectMySQL := False;
iConexe.HostName := iIP;
iConexe.Database := iDB;
try
iConexe.Connect;
ConnectMySQL := True;
except
on E:Exception do begin
showMessage('Cannot Conect' + Chr(10) + Chr(13) + E.Message);
end;
end; { except }
Screen.Cursor := crDefault;
end;

but if server is down, program crashes without any message
the same problem is when is missing libmysq.dll
any idea, what is wrong ?
thanx thom

Posted: 16.07.2008, 22:04
by mdaems
No idea, but can you please post a working (= failing :) ), very simple test project? So I can test/debug it myself. Just zip the source and attach it to this thread. You don't have to include the exe or dll's.

Mark

Posted: 17.07.2008, 08:16
by thcom
i am sorry, its little weird, i create litle app with this try connect except block, and everything works fine, but in my big application shows exception only in Delphi IDE in runtime shows UNKNOWN exception :( should i setupo something in delphi menu tools - debug options - OS Exceptions ?
i'm using delphi 7 enterprise with SP1, Win XP x64 Sp2, XAMPP 1.6.7 with MySQL 5.0.51.

Posted: 18.07.2008, 07:35
by zippo
I usually use error string parsing. It works perfectly for my needs.

Code: Select all

  except
    on E: Exception do begin
      Result:=false;
      KnownError:=false;
      if Pos('Access denied for user',E.Message)>0 then begin // Unknown user
        KnownError:=true;
        ErrMsg:='Wrong user';
      end;
      if Pos('Can''t connect to',E.Message)>0 then begin
        KnownError:=true;
        ErrMsg:='Unknown server';
      end;
      if Pos('Unknown database',E.Message)>0 then begin
        KnownError:=true;
        ErrMsg:='Unknown database';
      end;
      if not KnownError then
        ErrMsg:=E.Message;
      ShowMessage(ErrMsg);
    end;
  end;