Can results from a catch block be passed to methods inside the try block?

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();

Answers


No. You will need to isolate the correct block of code like you have done in your second example.

You can nest of course:

HttpWebResponse response = null;

try {
    try {
        response = (HttpWebResponse)request.GetResponse();
    } catch (WebException e) {
        response = (HttpWebResponse)e.Response;
    }

    if (response == null) return false;

    aDoc.Load(response.GetResponseStream());
} finally {
    if (response != null) response.Close();
}

No,

If a WebException was thrown, it has left the try block, and no further code in the try block will be executed, only what is in the finally block.


The answer is yes and no - since the code that passes the response to aDoc.Load() is the last statement, the exception can only have happened either on or above that line.

Assuming aDoc.Load() never throws WebException, then the answer is no

If aDoc.Load() throws WebException then the exception could come from that method - so technically aDoc.Load() could have been passed the result from the GetResponse() method and the exception could have been thrown inside aDoc.Load() - depends on aDoc.Load() implementation though

An exception stops code execution so once the exception is thrown, the next piece of code to be executed is that which is in the catch block (or any catch block further up the stack if there isn't one present) and then the finally is run

My assumption is that aDoc.Load() isn't any kind of web method (and is maybe your own class) so you should be best educated on whether that throws WebException, but the answer is probably 'no'


Need Your Help

Retrieving one record from the database

php arrays error-handling record fetch

Just trying to display one record from the database... But receiving an error (when fetching the array)

How to maximize Firefox dimensions with PHPUnit Selenium WebDriver

php firefox selenium webdriver phpunit

So I've been wondering how I set the browser dimensions to a certain point.