0

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;
Dutchman
  • 183
  • 1
  • 11
  • Plese post the code in which you tried using try stamtents. Also keep in mind that when you are running your program through debugger the debuger will always stop on exceptions unless you added certain exception type as exception to be ignored. And yes the debugger will stop at exception even when you have code to propely handle them becouse it detects the exception before that code is entirely executed. In such case just press F9 for your program to continue. If you have proper code for handling exceptions your cusom message will be shown othevise default Exception message will pop up. – SilverWarior Feb 26 '15 at 09:36
  • As @SilverWarior said, in debugger you can see the raised exception, including its type in the dialog like e.g. ` raised exception class EIdReadTimeOut with message `. The important part for you is the exception class. You can then write an exception handler that will do something when an exception of that class will be raised, e.g. `try..except on E: EIdReadTimeOut do DoSomething; on E: EIdFTPException do DoSomethingElse; ... end;`. An example skeleton, but for `TIdHTTP` is e.g. in [`this post`](http://stackoverflow.com/a/13954599/960757). – TLama Feb 26 '15 at 10:15
  • @TLama: There is no `EIdFTPException` in Indy. And depending on when `EIdReadTimeOut` is raised, `TIdFTP` may or may not be handling it internally. – Remy Lebeau Feb 26 '15 at 18:48
  • @Remy, that was just an example. And there is the `EIdFTPException` exception class. It's a common ancestor for exceptions raised by `TIdFTP`. – TLama Feb 27 '15 at 21:05
  • @TLama: my bad, I see `EIdFTPException` now. However, it (and its descendants) are only raised for errors that occur within `TIdFTP` itself (bad property/parameter values, invalid states, etc), not for error messages that are returned by the server in response to FTP commands. Those are handled by `EIdReplyRFCError` (and descendants). – Remy Lebeau Feb 27 '15 at 21:18

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770