0

I have this code:

public static string HttpGet(string URI)
    {
        System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
        System.Net.WebResponse resp = req.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        return sr.ReadToEnd().Trim();
    }
        try
        {
            SetInterval(() =>
            {
                string r = HttpGet("http://www.example.com/some.php?Username= Z&Status=On");
            }, 10000);
        }
        catch (WebException) { MessageBox.Show("No Network!"); }

What the Setinterval() does in retry run the code every 10000 milliseconds. But If I am not connected to internet, it gives me a WebException error. But it seems I can't even handle it. catching the exception still gives me the same error. Is there any way to just say 'Do nothing' when the error occurs?

P.S I am new to C#.

EDIT: Here's the code for setinterval:

public static IDisposable SetInterval(Action method, int delayInMilliseconds)
    {
        System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds);
        timer.Elapsed += (source, e) =>
        {
            method();
        };

        timer.Enabled = true;
        timer.Start();

        // Returns a stop handle which can be used for stopping
        // the timer, if required
        return timer as IDisposable;
    }
wingerse
  • 3,670
  • 1
  • 29
  • 61
  • What error are you seeing? No Network is what you expect on a WebException. If you get a different kind of Exception thrown, then that will still throw, because you're only catching WebException. If you're running this through Visual Studio, then you will still see an exception but then you should get your message, again assuming it's WebException. – tbddeveloper Sep 10 '14 at 17:44
  • This is the error I am still getting: `An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code Additional information: The remote name could not be resolved: 'example.com' If there is a handler for this exception, the program may be safely continued.` – wingerse Sep 10 '14 at 17:48

1 Answers1

2

You're catching the exception when you invoke SetInterval (which itself probably never throws a WebException), but not in the anonymous function which is being executed in the context of that interval. Move your exception handling into that function:

SetInterval(() =>
{
    try
    {
        string r = HttpGet("http://www.example.com/some.php?Username= Z&Status=On");
    }
    catch (WebException) { MessageBox.Show("No Network!"); }
}, 10000);
David
  • 208,112
  • 36
  • 198
  • 279
  • Why doesn't the exception bubble up in this case? – tbddeveloper Sep 10 '14 at 17:47
  • 2
    @Hammerstein: I'm not familiar with the implementation of `SetInterval` here, but presumably it offloads to a separate thread (or at least something analogous to that) and returns control to the calling code. Which would mean that the `try` block completes without error before the anonymous function is ever executed. They're not on the same stack anymore, so the exception doesn't move up that stack. (The OP can examine the stack trace of the exception to confirm this.) – David Sep 10 '14 at 17:50
  • Thanks David, putting the try block inside the setinterval did it.! – wingerse Sep 10 '14 at 17:53