I created a program that automatically connects to our local server and downloads updates, here is the code:
// Connect to web server and download ToBeInstalled.ini
Url := 'http://'+IPAdd+'/ToBeInstalled.ini';
MS := TMemoryStream.Create
try
try
http.Get(url, MS);
code := http.ResponseText;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode;
end;
MS.SaveToFile(UserPath + 'ToBeInstalled.ini');
finally
http.Free();
end;
The program works quite well while in the office but when users are home and cannot reach the server or the server is not available the get "socket error # 10061'
I have no idea how to catch that one and the worse is that the program stop execution all together after that error message is displayed. Do you have any idea how to fix that. Thank you so much.
Your exception handler is only catching EIdHTTPProtocolException
exceptions specifically, but there are several other types of exceptions that can be raised as well, including EIdSocketError
. You need to update your handler accordingly, or just have it catch all possible exceptions instead of looking for specific types. Since you say an uncaught exception caused your entire app to fail (which means you have bigger problems to deal with than just TIdHTTP
), you should also update the code to handle exceptions raised by TMemoryStream
as well.
Try this:
// Connect to web server and download ToBeInstalled.ini
Url := 'http://'+IPAdd+'/ToBeInstalled.ini';
try
MS := TMemoryStream.Create
try
http.Get(url, MS);
code := http.ResponseText;
MS.SaveToFile(UserPath + 'ToBeInstalled.ini');
finally
MS.Free;
end;
except
on E: EIdHTTPProtocolException do begin
code := http.ResponseCode;
end;
on E: Exception begin
// do something else
end;
end;