Catching Indy IdUDPClient.SendBuffer error lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Dzandaa · 2023-03-09 15:49 Hi, Using Lazarus 2.2.4 and Indy on Windows and Linux. I have an ESP8266 server that respond to an UDP command and send back Data. I have an Indy IdUDPClient that send command and receive the data. All works good except if I close the server or if I connect when  the IP and port are wrong, IdUDPClient.SendBuffer crash. I want to catch the error and close correctly. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    MyUDPClient.Host := IP;   MyUDPClient.Port := Port;   MyUDPClient.Connect;  // No error even if the IP or Port doesn't exist    ...   MyUDPClient.SendBuffer(DataSend); // I want to catch the error   Any ide? Thank you. ---------------------------------------------------------------------- Warfley · 2023-03-09 16:00 It's UDP, so you can't know if the message reached it's destination. Example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  sock: TSocket;  Res: SizeInt;begin  sock := UDPSocket(stIPv4);  Res := SendStrTo(sock, '153.2.111.10', 5917, 'Test'); // Made up IP and port  WriteLn(Res); // Result: 4 = Length('Test')  CloseSocket(sock);end.As you can see, when sending data to this made up ip and port, where there is no server behind, it still works as expected and tells me that 4 bytes have been sent, even though they never reached any destination. ---------------------------------------------------------------------- Dzandaa · 2023-03-09 16:16 Hi, Yes, I know that you can't rely on UDP and send data to an unknown IP, All I want to know if it is possible to catch the IdUDPClient.SendBuffer error with try, except finally. Thank you. ---------------------------------------------------------------------- Warfley · 2023-03-09 16:18 If an exception is thrown, you you probably can just use try-except. But you should note that lazarus breaks on exceptions anyway, if they are enclosed in try-except or not. This is just a debugging functionality and will not occur in production code running without a debugger. You can disable handling of certain exception types to avoid this. Otherwise, just click the continue button when the exception message pops up. ---------------------------------------------------------------------- Remy Lebeau · 2023-03-10 02:08 Quote from: Warfley on March 09, 2023, 05:00:24 pmIt's UDP, so you can't know if the message reached it's destination. Actually, you can, but not immediately.  When you send a packet to a non-existent IP/port, the network is likely to send back an ICMP packet explaining that the host/port was unreachable.  If you Connect() a UDP socket to a peer, and the socket internally receives such an ICMP packet after a send to that peer, then subsequent sends on that same peer will fail with an error that you can catch, as long as the socket remains "connected" to that peer. Quote from: Dzandaa on March 09, 2023, 05:16:56 pmAll I want to know if it is possible to catch the IdUDPClient.SendBuffer error with try, except finally. Of course it is possible.  It is just a normal 'raise'd exception, like any other.  Why do you think it is not possible to catch it? ---------------------------------------------------------------------- Dzandaa · 2023-03-10 09:50 Hi, Thank you Warfley, I forgot that the debugger showed an alert message when using try, except, etc... Thank you. B->