locked
HttpWebRequest throws exception on NotModified RRS feed

  • Question

  • User-1784647599 posted

    In my application I'm using HttpWebRequest to fetch some XML from a web server. To avoid fetching the XML when it has not been updated, I'm caching the XML and I'm using the HttpWebRequest.IfModifiedSince.

    My problem is though that HttpWebRequest throws a WebException on GetResponse() if the StatusCode is different from HttpStatusCode.Ok.

    Is there any way to keep HttpWebRequest from throwing an exception here? Exceptions are expensive to throw and it's bad practice to use exceptions for flow control.

     

    Thursday, November 2, 2006 9:47 AM

All replies

  • User1001868398 posted

    First, are you sure the webservice sets the flag is modify since well?

    Second, exceptions are a wonderful thing, can you put the code between the try and catch and if you receive the exception you know is not modified!

    GetResponse() always returns an exception besides when is sucess!

    Thursday, November 2, 2006 10:25 PM
  • User-1784647599 posted

    First, are you sure the webservice sets the flag is modify since well?

    Yes. That's how I saw the exception in the first place. 

    Second, exceptions are a wonderful thing, can you put the code between the try and catch and if you receive the exception you know is not modified!

    GetResponse() always returns an exception besides when is sucess!

    Using throw and catch clauses for flow control is bad practice. Exceptions are expensive to throw, and they should only be used for errors. In other parts of the .NET framework I have seen ways to specify wether or not you want an exception, but I can't see that here.

    Here is a working example of code using exceptions for flow control. It works, but it's horrible:

                int[] array = new int[100];
                try
                {
                    for (int i = 0; ; i++)
                    {
                        int j = array[i];
                    }
                } catch (IndexOutOfRangeException ioe)
                {
                    //Ok. the loop is done
                }


     

    Friday, November 3, 2006 3:01 AM
  • User1001868398 posted
    Sorry the design from MS with that class is an exception when error!
    Friday, November 3, 2006 11:52 AM