C# handling errors

Associate
Joined
30 Dec 2005
Posts
415
Just a quick query... one of my C# programs keeps freezing when the following code goes into the catch statement. I'm basically wondering what is the best method of writing the below code so that if it fails the program won't crash...

Code:
        void updateme(object sender, EventArgs e)
        {
            String URLString = "http://www.routehiker.org.uk/api/help/exeinterface.php?update&userid=" + settings.Default.userid;
            try
            {
                XmlTextReader reader = new XmlTextReader(URLString);
                string[] userdetails;
                userdetails = new string[7];
                int i = 0;
                while ((reader.Read() && i < 7))
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Text:
                            if (reader.Value != "0")
                            {
                                userdetails[i] = reader.Value;
                                i++;
                            }
                            else
                            {
                                i = 7;
                            }
                            break;
                    }
                }
                if (userdetails[0] != null)
                {
                    update.Stop();
                    this.Show();
                    MessageBox.Show(userdetails[1] + " " + userdetails[2] + " has started a conversation");
                    listbox_details.Items.Add("A conversation has started with " + userdetails[1] + " " + userdetails[2] + ".");
                    Form Other = new messenger(userdetails);
                    Other.ShowDialog();
                    listbox_details.Items.Add("The conversation with " + userdetails[1] + " " + userdetails[2] + " has ended.");
                    update.Start();
                }
                return;
            }
            catch { MessageBox.Show("CATCH 4");  }
        }

Cheers all!
 
It runs the method every minute, but the error seems to occur quite randomly, the last time it occured after 4 hours.

The debug shows nothing at all, even though the program has completely frozen :confused:

All it does is bring up a messagebox saying CATCH 4, then it freezes before you can click ok.

Dunno if that helps or not!
 
Inquisitor said:
When it enters the breakpoint you can access the exception itself by going to the locals window (usually a the bottom of the screen unless you've changed it) at runtime, and the exception object will be listed as $exception.

If you have a look at the stack trace in the exception's information, it'll tell you the exact method call that failed.

Although, having said that, I can tell you with a fair amount of certainty that it's the new XmlTextReader(URLString) call that's failed. :)

That sounds ideal :)

I just tried to add break; in the catch {}, but it said it wasn't in a loop so it couldn't compile..do I need to put it somewhere else or is there some more code I need to add?

Sorry for the newbieness!
 
Ok I left it running for about 3 hours and eventually it froze and the debug produced this:
Code:
-		$exception	{"The operation has timed out"}	System.Exception {System.Net.WebException}
-		[System.Net.WebException]	{"The operation has timed out"}	System.Net.WebException
-		base	{"The operation has timed out"}	System.InvalidOperationException {System.Net.WebException}
-		base	{"The operation has timed out"}	System.SystemException {System.Net.WebException}
-		base	{"The operation has timed out"}	System.Exception {System.Net.WebException}
+		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
		HelpLink	null	string
+		InnerException	null	System.Exception
		Message	"The operation has timed out"	string
		Source	"System"	string
		StackTrace	"   at System.Net.HttpWebRequest.GetResponse()\r\n   at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials)\r\n   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)\r\n   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)\r\n   at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)\r\n   at System.Threading.CompressedStack.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)\r\n   at System.Xml.XmlTextReaderImpl.OpenUrl()\r\n   at System.Xml.XmlTextReaderImpl.Read()\r\n   at System.Xml.XmlTextReader.Read()\r\n   at Routy_Helper.Form1.updateme(Object sender, EventArgs e) in D:\\Visual Studio 2005\\Projects\\Routy Helper1\\Routy Helper1\\Form1.cs:line 331"	string
+		TargetSite	{System.Net.WebResponse GetResponse()}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
+		Static members		
+		Non-Public members		
		Response	null	System.Net.WebResponse
		Status	Timeout	System.Net.WebExceptionStatus
+		Non-Public members		
+		Data	{System.Collections.ListDictionaryInternal}	System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
		HelpLink	null	string
+		InnerException	null	System.Exception
		Message	"The operation has timed out"	string
		Source	"System"	string
		StackTrace	"   at System.Net.HttpWebRequest.GetResponse()\r\n   at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials)\r\n   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)\r\n   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)\r\n   at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)\r\n   at System.Threading.CompressedStack.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)\r\n   at System.Xml.XmlTextReaderImpl.OpenUrl()\r\n   at System.Xml.XmlTextReaderImpl.Read()\r\n   at System.Xml.XmlTextReader.Read()\r\n   at Routy_Helper.Form1.updateme(Object sender, EventArgs e) in D:\\Visual Studio 2005\\Projects\\Routy Helper1\\Routy Helper1\\Form1.cs:line 331"	string
+		TargetSite	{System.Net.WebResponse GetResponse()}	System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
+		Static members		
+		Non-Public members		
+		this	{Routy_Helper.Form1, Text: Routy Helper}	Routy_Helper.Form1
+		sender	{Interval = 10000}	object {System.Windows.Forms.Timer}
+		e	{System.EventArgs}	System.EventArgs
+		ex	{"The operation has timed out"}	System.Exception {System.Net.WebException}
		URLString	"http://www.routehiker.org.uk/api/help/exeinterface.php?update&userid=1"	string
 
Inquisitor said:
Looking at the stack trace info there, it's reader.Read() that's failing due to a timeout. Basically this just means your program couldn't connect to the server. There's not much you can do about this; just add a catch block that catches WebExceptions and does nothing (if you want it to ignore the fact that it couldn't contact the server that is).

Added that additional catch block and all seems to be working fine so far. Thanks for the help! :D
 
Back
Top Bottom