1
我得到一個System.Net.WebException
說:C#未處理的異常使用try catch塊
The remote server returned an error: (403) Forbidden.
這是我很期待,因爲無效的頭被傳遞與HTTP請求。但是,我的代碼似乎沒有像我期望的那樣捕獲異常。
下面是代碼:
private void callback(IAsyncResult result)
{
Debug.WriteLine("Callback");
HttpWebResponse response = null;
try
{
response = (result.AsyncState as HttpWebRequest).EndGetResponse(result)
as HttpWebResponse;
}
catch (WebException e)
{
Debug.WriteLine("Exception: " + e);
}
catch (Exception e)
{
Debug.WriteLine("Unknown exception: " + e);
}
}
爲什麼例外而不是抓?
這是否幫助? http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba – ojf 2014-10-06 19:12:42
可能是因爲這種異步的事情,不同的事情發生在不同的線程? – 2014-10-06 19:16:40