我的程序中有以下代码:
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
if (response == null)
return false;
aDoc.Load(response.GetResponseStream()); //Load the response into another object
}
catch (WebException e)
{
//404's are caught and are saved as the response.
//The reason being that 404's from this particular
// website still provide relevant information that needs
// extracting.
response = (HttpWebResponse)e.Response;
}
finally
{
response.Close();
}
我的问题是:如果捕获到 WebException,是否会将响应response = (HttpWebResponse)e.Response;
传递给aDoc.Load()
方法?
顺便说一句,在将更多代码移入 try-catch 块之前,我有以下代码。我认为添加finally
withClose()
会更安全,但我仍然想知道我是否应该首先更改任何内容。
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
response = (HttpWebResponse)e.Response;
}
if (response == null)
return false;
aDoc.Load(response.GetResponseStream());
response.Close();