locked
Can't Catch Exception RRS feed

  • Question

  • Hello,

    when I quering user's music library using following code it works perfectly but I found a strange exception when user's music library does not contain any folders i can't catch the exception why?

    and how i know music library doesnot have a folder included?

                       try
                        {
                            StorageFileQueryResult fileQuery = folder.CreateFileQueryWithOptions(queryOptions1);
                            uint count = await fileQuery.GetItemCountAsync();
                            if (count > 0)
                            {
                            }
                        }
                        catch (Exception)
                        {
                            throw;
                        }

    the control of program is not coming to following line and i can't caught exception

    if(count>0)

    the following image shows music librarys state when this occur


    Santana George

    Sunday, April 28, 2013 5:34 PM

All replies

  • I would assume that fileQuery or queryOptions1 is null that is why you are getting the exception when you get the file count
    • Edited by Ken Tucker Sunday, April 28, 2013 6:29 PM
    Sunday, April 28, 2013 6:21 PM
  • filequery is not null and I can't catch any exception here...

    Santana George

    Monday, April 29, 2013 5:06 AM
  • Try doing it this way:

                var fileQuery = folder.CreateFileQueryWithOptions(queryOptions1);
                var task = fileQuery.GetItemCountAsync();  //note: don't await this line
                try
                {
                    uint count = await task;
                    if (count > 0)
                    {
                    }
                }
                catch (Exception)
                {
                    throw;
                }
    I think that the exception in the GetItemCountAsync will get passed to the task variable where it will then be caught by the try block.  (I have not tried this myself.)


    • Edited by jrboddie Monday, April 29, 2013 6:19 AM
    Monday, April 29, 2013 6:18 AM
  • not working ...


    Santana George

    Monday, April 29, 2013 6:33 AM
  • Sorry.  It was just an after midnight guess on my part.  Go back to the original code.

    Is the issue that Visual Studio is catching the exception and not your code handler? If so, what happens if you click Continue on the VS exception dialog?

    Monday, April 29, 2013 11:38 AM
  • Thanks For your Help But

    I can't get any exception (means visual studio does not break or caught exception ) VS act like it is a logic error.

     I found another way to caught this exception just call

    var list = await folder.GetFoldersAsync();

    in a try catch VS Now Shows a Exception something like COM_Component Exception.

    but I don't like this type of coding. VS must throw a exception in my old code any way I need to Continue


    Santana George




    Monday, April 29, 2013 11:44 AM
  • Hi,

    I cannot reproduce your issue, I try to query a empty music library, but did not get any exception.

    Would you please show us the detail steps?

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 30, 2013 3:02 AM
  • using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.Storage;
    using Windows.Storage.Search;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace App17
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                try
                {
                var queryOptions1 = new QueryOptions();
                queryOptions1.FolderDepth = FolderDepth.Deep;
                queryOptions1.FileTypeFilter.Add("*");
                queryOptions1.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
                var fileQuery = KnownFolders.MusicLibrary.CreateFileQueryWithOptions(queryOptions1);
                uint count = await fileQuery.GetItemCountAsync();  //note: don't await this line
                    if (count > 0)
                    {
                        count += 45; //  Compiler is not reach here why?
                    }
                }
                catch (Exception)
                {
                    throw;
                }
    
            }
        }
    }
    

    run above code compiler won,t reach below show lines and won't produce any exception for us why?

    count += 45; //  Compiler is not reach here why?

    my compiler need to caught a exception but unfortunately it won't happening...


    Santana George

    Tuesday, April 30, 2013 3:13 AM
  • I tested this, and found count == 0

    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 30, 2013 4:02 AM
  • just try to show the count on a textblock like following code it won't display

    try { var queryOptions1 = new QueryOptions(); queryOptions1.FolderDepth = FolderDepth.Deep; queryOptions1.FileTypeFilter.Add("*"); queryOptions1.IndexerOption = IndexerOption.UseIndexerWhenAvailable; var fileQuery = KnownFolders.MusicLibrary.CreateFileQueryWithOptions(queryOptions1); uint count = await fileQuery.GetItemCountAsync();

    count += 45; tb.Text = count.ToString();// Compiler is not reach here why? } catch (Exception) { throw; }



    Santana George


    Tuesday, April 30, 2013 4:27 AM
  • Hi,

    I think it caused by the await keyword. It is asynchronous function, it means that you can not get the result immediate after run this function. You may wait for a while and get the result.

    Please take a look of
    http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 1, 2013 3:15 AM
  • I have waited more than 2 minutes but no result think it is a compiler error if there is no location to query compiler must return a exception.

    Santana George

    Wednesday, May 1, 2013 3:46 AM