exception - Handling an WebException in C# -
i have 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 setinterval() in retry run code every 10000 milliseconds. if not connected internet, gives me webexception error. seems can't handle it. catching exception still gives me same error. there way 'do nothing' when error occurs?
p.s new c#.
edit: here's code 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 stop handle can used stopping // timer, if required return timer idisposable; }
you're catching exception when invoke setinterval
(which never throws webexception
), not in anonymous function being executed in context of interval. move exception handling function:
setinterval(() => { try { string r = httpget("http://www.example.com/some.php?username= z&status=on"); } catch (webexception) { messagebox.show("no network!"); } }, 10000);
Comments
Post a Comment