2013-04-28 25 views
1

我創建的自動連接到本地服務器,並下載更新程序,這裏是代碼:TIdHTTP異常處理

// 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; 

程序工作得很好,而在辦公室,但是當用戶回家並不能達到服務器或服務器不可用的get「套接字錯誤#10061'

enter image description here

我不知道如何捕捉一個更壞的消息是,程序停止執行該誤碼M畢竟在一起顯示消息。你有什麼想法如何解決這個問題。非常感謝。

回答

10

您的異常處理程序僅特別捕獲EIdHTTPProtocolException異常,但也可以引發其他幾種類型的異常,包括EIdSocketError。您需要相應地更新您的處理程序,或者只是讓它捕獲所有可能的異常,而不是查找特定的類型。既然你說一個未捕獲的異常導致你的整個應用程序失敗(這意味着你需要處理的問題不僅僅是TIdHTTP),你還應該更新代碼以處理由TMemoryStream引發的異常。

試試這個:

// 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; 
+0

這做到了。非常感謝。 – Cor4Ever 2013-04-28 23:34:52

+5

更多關於'TIdHTTP'異常的信息,請參閱['here'](http://stackoverflow.com/a/13954599/960757)。 [1] – TLama 2013-04-29 06:46:08