locked
Unusual Exception

    Question

  • I have the following function in my program that takes list called reminders and puts it into a ListView:

            private void loadrem()
            {
                try
                {
                    listviewrem.Items.Clear();
                    for (int i = 0; i < reminders.Count(); i++)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Name = i.ToString();
                        lvi.Content = reminders[i];
                        lvi.FontSize = 36;
                        listviewrem.Items.Add(lvi);
                    }
                }
                catch (Exception ex)
                {
                    textboxdesc.Text = ex.ToString();
                }
            }

    The function is executed when the page loads.  It is also executed again when 'reminders' changes.  Nothing happens when the page loads, but after it is executed again, the app quits and goes to the following code in App.g.i.cs:

    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
                UnhandledException += (sender, e) =>
                {
                    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
                };
    #endif

    It also highlights 'global::System.Diagnostics.Debugger.Break();'  It does not show a window describing the exception.  This also occurs when it is executed after the page loads and reminders hasn't changed.  I debugged loadrem() line by line and the app quits only after the function has finished executing.  Other than that, the function executes properly.

    • Moved by Mike Danes Monday, August 04, 2014 11:53 AM store app specific
    Monday, August 04, 2014 2:47 AM

Answers

  • When the debugger stops on the line containing Break you can see the exception details by hovering over the 'e' parameter of the lambda expression or by using debugger's 'watch' functionality.

    I tried something like you describe and the exception message is "The name already exists in the tree: 0.". I suspect that this is a bug in the Windows XAML library because the names should have probably be removed when you did Items.Clear().

    You could probably avoid this problem by not setting the Name property of the item or better, by using data binding and data templates to populate the list. At least that's how I'd do this in WPF.

    Since this is specific to Windows Store apps I'll move your thread to the appropriate forum where you may get better support.

    • Marked as answer by qwertyuiop5 Monday, August 04, 2014 2:43 PM
    Monday, August 04, 2014 11:52 AM

All replies

  • The code is failing some place else, but is captured in the exception handler because you are missing an exception handler where the code actually is failing.  When an exception occurs the Net Library searches up the execution stack until the first exception handler is found.  VS will automatically add a default exception handler before main() so there is at least one exception handler in the application.  When an exception handler is missing where the failure actually occurs it will be captured in another exception handler.

    You are failing early in the code or in sub function of the form class.  Since you are failing in the load method I would start by checking the constructor.  Add more break points in the code and continue stepping until you find the real location of the error.


    jdweng

    Monday, August 04, 2014 9:11 AM
  • The function works when the program loads, but only throws an exception when it is executed again.  After debugging it line by line, the whole function executes as it should.  After it executes the line with the closing bracket of the function, it throws the exception.
    Monday, August 04, 2014 11:26 AM
  • I found that if I take out the line 'listviewrem.Items.Add(lvi);' the exception is not thrown.
    Monday, August 04, 2014 11:33 AM
  • When the debugger stops on the line containing Break you can see the exception details by hovering over the 'e' parameter of the lambda expression or by using debugger's 'watch' functionality.

    I tried something like you describe and the exception message is "The name already exists in the tree: 0.". I suspect that this is a bug in the Windows XAML library because the names should have probably be removed when you did Items.Clear().

    You could probably avoid this problem by not setting the Name property of the item or better, by using data binding and data templates to populate the list. At least that's how I'd do this in WPF.

    Since this is specific to Windows Store apps I'll move your thread to the appropriate forum where you may get better support.

    • Marked as answer by qwertyuiop5 Monday, August 04, 2014 2:43 PM
    Monday, August 04, 2014 11:52 AM
  • How many times does it go through the for loop before failing?  Can you post code for listviewrem.

    jdweng

    Monday, August 04, 2014 12:27 PM
  • I changed the code which added the items to the list view.  This code seems to work.

            private void loadlistview()
            {
                listviewrem.Items.Clear();
                foreach (string s in reminders)
                {
                    listviewrem.Items.Add(s);
                }
            }

    Monday, August 04, 2014 2:45 PM