如何捕获system.xml.xmlexception

时间:2014-07-03 10:38:49

标签: c# .net visual-studio windows-phone-8

我发布了一个针对Windows Phone 8智能手机的小型免费应用程序(以了解C# - 所以我是编程的初学者)。我的很多用户似乎对这些功能非常满意,但其中一些似乎不断发生随机崩溃(例如在加拿大:http://www.windowsphone.com/en-ca/store/app/picture-of-the-day/fc977a34-c09d-4c70-8a7b-66b6f09ab7f0) 我从来没有在我的测试设备上遇到过这样的崩溃,并试图通过添加更多的try-catch语句来摆脱它们 - 但没有成功。

崩溃报告声明如下:

Frame Image Function Offset        
0 system_xml_ni System.Xml.XmlTextReaderImpl.Throw 0x00000036    
1 system_xml_ni System.Xml.XmlTextReaderImpl.ParseDocumentContent 0x00000438    
2 system_xml_ni System.Xml.XmlTextReaderImpl.Read 0x00000036    
3 system_xml_linq_ni System.Xml.Linq.XDeclaration..ctor 0x00000072    
4 system_xml_linq_ni System.Xml.Linq.XDocument.Load 0x0000010a    
5 system_xml_linq_ni System.Xml.Linq.XDocument.Load 0x00000042    
6 system_xml_linq_ni System.Xml.Linq.XDocument.Load 0x00000006    
7 phoneapp1_ni PhoneApp1.MainPage+__c__DisplayClassb._doLoadURL_b__6 0x00000040    
8 system_net_ni System.Net.WebClient.OnOpenReadCompleted 0x00000010    
9 system_net_ni System.Net.WebClient.OpenReadOperationCompleted 0x00000034`

我认为实际的代码是负责的:

try 
{
    WebClient client = new WebClient();
    client.OpenReadAsync(new Uri(Url, UriKind.Absolute));
    client.OpenReadCompleted += (sender, e) =>
    {
        if (e.Error != null)
        {
            return;
        }
        else
        {
            System.Xml.Linq.XDocument xmlDoc = XDocument.Load(e.Result);
            IEnumerable<string> strTestURL = from node in xmlDoc.Descendants("url") select node.Value;
            IEnumerable<string> strTestDescription = from node in xmlDoc.Descendants("copyright") select node.Value;
            IEnumerable<string> strTestDate = from node in xmlDoc.Descendants("enddate") select node.Value;
            string strURL = "http://www.bing.com" + strTestURL.First();
            strURL = strURL.Replace("1366x768", "800x480");
            Global.URL1 = strURL;
            Global.URLs[i] = strURL;
            Global.Descriptions[i] = strTestDescription.First();
            Uri Uri = new Uri(Global.URLs[i], UriKind.Absolute);
            Imageallgemein.Source = new BitmapImage(Uri);
            Imageallgemein.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(onImageTap);
            Imageallgemein.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(onImageTap);
            Description.Text = Global.Descriptions[i];
            string Year = strTestDate.First().Substring(0, 4);
            string Month = strTestDate.First().Substring(4, 2);
            string Day = strTestDate.First().Substring(6, 2);
            Date.Text = Day + "." + Month + "." + Year;
        }
    };
}
catch
{
    MessageBox.Show(AppResources.Abort, AppResources.msgBoxUrlLoadError, MessageBoxButton.OK);
}

try-catch似乎没有效果,我希望有人可以为我解决这个问题。

2 个答案:

答案 0 :(得分:3)

e.Result提供的XML一定存在问题。有关这方面的详细信息可能在XmlException消息中,但您只包含了堆栈跟踪的一部分。

您必须首先弄清问题是什么,如果您无法在自己的系统上重现问题,则可能需要在调用XDocument.Load之前添加一些记录。

您还可以添加异常处理程序,但这不能解决问题,但会使您的应用程序更加强大,并且如果出现意外情况,则可以提供稍好的用户界面。您所做的是围绕对WebClient方法的调用添加异常处理程序,但您没有捕获client.OpenReadCompleted处理程序抛出的异常。这是一个将在线程池线程上执行的异步回调,此线程抛出的任何未捕获的异常都将终止您的应用。

您需要使用以下代码处理异常:

client.OpenReadCompleted += (sender, e) =>
{
    try
    {
        if (e.Error != null)
        {
            return;
        }
        else
            ....
    }
    catch (Exception ex)
    {
        .... log and report the exception to allow the app to continue
    }
 }

如果您决定向您的应用添加日志记录,那么如果您记录ex.ToString()返回的整个文本,对您来说非常有用。这将为您提供问题的良好文本描述,包括内部异常和完整堆栈跟踪。

答案 1 :(得分:2)

通常情况下,catch(Exception)catch(system.xml.XmlException)的好习惯。 但是在你的else块中加入了一个try-catch,因为这是一个异步事件,如果发生了异常,那么异常就不会被捕获:

try 
{
    WebClient client = new WebClient();
    client.OpenReadAsync(new Uri(Url, UriKind.Absolute));
    client.OpenReadCompleted += (sender, e) =>
    {
        if (e.Error != null)
        {
            return;
        }
        else
        {
           try
           {
              System.Xml.Linq.XDocument xmlDoc = XDocument.Load(e.Result);
              IEnumerable<string> strTestURL = from node in xmlDoc.Descendants("url") select node.Value;
              IEnumerable<string> strTestDescription = from node in xmlDoc.Descendants("copyright") select node.Value;
              IEnumerable<string> strTestDate = from node in xmlDoc.Descendants("enddate") select node.Value;
              string strURL = "http://www.bing.com" + strTestURL.First();
              strURL = strURL.Replace("1366x768", "800x480");
              Global.URL1 = strURL;
              Global.URLs[i] = strURL;
              Global.Descriptions[i] = strTestDescription.First();
              Uri Uri = new Uri(Global.URLs[i], UriKind.Absolute);
              Imageallgemein.Source = new BitmapImage(Uri);
              Imageallgemein.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(onImageTap);
              Imageallgemein.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(onImageTap);
              Description.Text = Global.Descriptions[i];
              string Year = strTestDate.First().Substring(0, 4);
              string Month = strTestDate.First().Substring(4, 2);
              string Day = strTestDate.First().Substring(6, 2);
              Date.Text = Day + "." + Month + "." + Year;
           }
           catch (XmlException)
           {
                MessageBox.Show(AppResources.Abort, AppResources.msgBoxUrlLoadError, MessageBoxButton.OK);
           }
        }
    };
}
catch (Exception)
{
    MessageBox.Show(AppResources.Abort, AppResources.msgBoxUrlLoadError, MessageBoxButton.OK);
}