I have the following code in my program:
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();
}
My question is: If a WebException is caught, will the response from response = (HttpWebResponse)e.Response; be passed to the aDoc.Load() method?
As an aside, I had this following code before moving more of it into the try-catch block. I figured adding a finally with Close() would be safer, but I’m still wondering if I should have changed anything at all in the first place.
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();
No. You will need to isolate the correct block of code like you have done in your second example.
You can nest of course: