Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle exceptions TIdTcpServer, TIdTcpClient
(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.


Messages In This Thread
RE: How to handle exceptions TIdTcpServer, TIdTcpClient - by rlebeau - 05-08-2019, 05:50 PM

Forum Jump:

Users browsing this thread: 1 Guest(s)