Dutchman
Dutchman

Reputation: 183

Delphi FTP timeout exception

I'm using Indy's FTP.

It works perfectly with what I want, my only issue is that if the system isn't connected to the Internet or the server is down then it displays a TimeOut exception on screen.

I can't seem to be able to find out how to catch the exception. I just want it to, instead of show an error, be able to show my own message to reassure the user that there is no issue. If that made sense.

I've used try excepts before and they work just not here it seems. Could someone give me an idea or some example code of where and how to write the exception catcher here?

Thanks

EDIT: Sorry, I was away from my normal PC so couldn't post code. Actually I just retried and it did catch it, thanks for the note about the debugger always showing the exception. However, after my custom message I also get a 'Connection Closed Gracefully' message. (I ran it outside of debugger) How/Where can I catch/stop that one?

Also, it sometimes returns a message from my server, such as 'Cant connect more than 3 times on same account' or whatever. Can I stop/catch that aswell? Thanks Here:

Form1.ftp.Host := 'HOSTNAME';
            Form1.ftp.User := 'USERNAME';
            Form1.ftp.password := 'PASSWORD';
            Try
                    Form1.ftp.Connect;
            Except on E : Exception do
                    begin
                            ShowMessage('Timeout Error, dont worry');
                    end;
            end;

Upvotes: 0

Views: 2251

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598001

Like most components in Indy, TIdFTP does everything synchronously, and errors are reported as exceptions. Standard try/except blocks work just fine. Indy is designed for that.

If you are seeing a Connection Closed Gracefully message appear when running your app outside of the debugger, it means you tried to perform a socket operation after the socket was already disconnected, and you did not catch the EIdConnClosedGracefully exception that was raised into your code. For instance, if Connect() fails, it calls Disconnect() internally before raising an exception to you. Don't call any other TIdFTP methods in that situation, other than Connect() again if needed.

As for error messages sent by the FTP server, they are usually reported by raising EIdReplyRFCError (or derived) exceptions, which you can catch in your code.

Upvotes: 2

Related Questions