Share via


catch (TimeoutException te) not catching "The operation has timed out" exception

Question

Monday, July 5, 2010 3:48 PM

What am I missing here? 

 

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strSomeUrl);
        request.Timeout = 2; // I want it to time out for this test

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        }
        catch (TimeoutException t)
        {
            string myexceptionT = t.Message; // breakline 1:  break on this line IS NOT hit
        }
        catch (Exception exc)
        {
            string myexceptionE = exc.Message; // breakline 2:  break on this line IS hit; myexceptionE == "The operation has timed out"
        }

 

Why is it stopping on the second break instead of the first?

All replies (2)

Monday, July 5, 2010 7:45 PM ✅Answered

You're catching the wrong exception, the list of exceptions this thing throws are listed here

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx 


Monday, July 5, 2010 7:49 PM ✅Answered

I think it will throw WebException not TimeoutException when timeout.