Atozed Forums
How to handle exceptions TIdTcpServer, TIdTcpClient - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Indy (https://www.atozed.com/forums/forum-8.html)
+--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html)
+--- Thread: How to handle exceptions TIdTcpServer, TIdTcpClient (/thread-1063.html)



How to handle exceptions TIdTcpServer, TIdTcpClient - whitekn3 - 05-08-2019

I recently took over development/maintenance on a project written in Builder C++ 6 and just converted to Indy 10.   Some quirky unexpected problems occurred that seem to be related to the error handling.  I am pretty much a novice when it comes to Indy so I'm struggling to figure it all out.

Web searches found several comments by RLebeau that said "let the server handle the exceptions".   I looked, but never found, for code samples that would clarify what he meant. 

In this case TIdTcpServer Execute calls DoPassthruServerExecute, which calls HandleFirstCallerMessage, both of which have try ... catch sections.  There is also a TIdTcpClient and a ReceiveThread (TThread, TIdContext which is passed the server context) that has a try .. catch block, and which catches EIdConnClosedGracefully.

I've attached a stripped down code that has all the essential code and error handling.  I would greatly appreciate it if someone could take a look at it and let me know if they see any obvious problems or design issues.

Thanks.


RE: How to handle exceptions TIdTcpServer, TIdTcpClient - rlebeau - 05-08-2019

(05-08-2019, 01:24 PM)whitekn3 Wrote: I recently took over development/maintenance on a project written in Builder C++ 6 and just converted to Indy 10.   Some quirky unexpected problems occurred that seem to be related to the error handling.  I am pretty much a novice when it comes to Indy so I'm struggling to figure it all out.

Indy uses blocking operations, and raises exceptions on errors. You handle Indy exceptions at the call site with a try/except block, like you do any other kind of exception.

(05-08-2019, 01:24 PM)whitekn3 Wrote: Web searches found several comments by RLebeau that said "let the server handle the exceptions".   I looked, but never found, for code samples that would clarify what he meant. 

Because there is no code involved for that. You simply let an exception be raised and don't try to catch it. In the case of TIdTCPServer, as the exception propagates up the call stack, it will eventually be caught by an internal try/except block inside of TIdTCPServer, which will then trigger its OnException event handler (if assigned) and close the socket connection for the thread that raised the exception.

(05-08-2019, 01:24 PM)whitekn3 Wrote: In this case TIdTcpServer Execute calls DoPassthruServerExecute, which calls HandleFirstCallerMessage, both of which have try ... catch sections. There is also a TIdTcpClient and a ReceiveThread (TThread, TIdContext which is passed the server context) that has a try .. catch block.

Please show the actual code you are having trouble with.

(05-08-2019, 01:24 PM)whitekn3 Wrote: which catches EIdConnClosedGracefully

That is not the only kind of exception that Indy can raise. Indy has many exception types available. Some are specialized to specific error cases (like EIdConnClosedGracefully), but others are not. At the very least, you would need to also catch EIdSocketError to catch other kinds of socket error besides just "closed gracefully". Or, just catch EIdException (ALL Indy exceptions derive from this) or even SysUtils.Exception generally instead.

(05-08-2019, 01:24 PM)whitekn3 Wrote: I've attached a stripped down code

Actually no, you did not.


RE: How to handle exceptions TIdTcpServer, TIdTcpClient - whitekn3 - 05-14-2019

[attachment=103]

Sorry, I thought I had attached code. I just reattached it, and as far as I can tell it is now "attached."