locked
StorageFolder->GetFileAsync() throws exception when file doesn't exist in Win8 Store App

    Question

  • StorageFolder->GetFileAsync() throws exception when file doesn't exist in Win8 Store App. And I have already use simple try /catch operation, but the  exception  still throw out and catch operation can't got it. The simple code as below.

    String^ FileName = "1111.txt";
     try
     {
      task<StorageFile^> GetFileTask(KnownFolders::DocumentsLibrary->GetFileAsync(FileName));
      GetFileTask.then([this](StorageFile^ file)
      {
       try{
        OutputDebugString( file->Path->Data());

       }catch(Exception^ ex)
       {
        OutputDebugString(ex->Message->Data());
       }
      });
     }catch(Exception^ ex)
     {
      OutputDebugString(ex->Message->Data());
     }


    • Edited by TonyChi Tuesday, October 16, 2012 7:12 AM
    Tuesday, October 16, 2012 7:06 AM

Answers

  • Hi TonyChi,

    WinRT is completely asynchronous. It does not return the result immediately rather than returns the way of result. So some where you have to wait for the result and should be placed it under try/catch block.

    Actually you missed the proper sequence of asynchronous task. I fixed it. Please use following code.

    	  

    String^ FileName = "1111.txt";

    task<StorageFile^> GetFileTask(KnownFolders::DocumentsLibrary->GetFileAsync(FileName)); GetFileTask.then([this](StorageFile^ file) { try{ OutputDebugStringW(L"Path : "); OutputDebugStringW( file->Path->Data()); OutputDebugStringW(L"\n\n"); } catch(Exception^ ex) { OutputDebugStringW(L"ex = "); OutputDebugStringW(ex->Message->Data()); OutputDebugStringW(L"\n\n"); } }).then( [] (task<void> previousTask) { try{ previousTask.get(); //Get the result of task by forcing it. } catch(Exception^ ex) { OutputDebugStringW(L"exp = "); OutputDebugStringW(ex->Message->Data()); OutputDebugStringW(L"\n\n"); } });

    Till now, if you will get the pop-up window with exception, please see the following thread

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/de134da2-c8ae-4e60-847d-57642b4123c3


    • Edited by Mokarrom Hossain Tuesday, October 16, 2012 11:50 AM
    • Marked as answer by TonyChi Wednesday, October 17, 2012 6:33 AM
    Tuesday, October 16, 2012 11:35 AM

All replies

  • Hi TonyChi,

    WinRT is completely asynchronous. It does not return the result immediately rather than returns the way of result. So some where you have to wait for the result and should be placed it under try/catch block.

    Actually you missed the proper sequence of asynchronous task. I fixed it. Please use following code.

    	  

    String^ FileName = "1111.txt";

    task<StorageFile^> GetFileTask(KnownFolders::DocumentsLibrary->GetFileAsync(FileName)); GetFileTask.then([this](StorageFile^ file) { try{ OutputDebugStringW(L"Path : "); OutputDebugStringW( file->Path->Data()); OutputDebugStringW(L"\n\n"); } catch(Exception^ ex) { OutputDebugStringW(L"ex = "); OutputDebugStringW(ex->Message->Data()); OutputDebugStringW(L"\n\n"); } }).then( [] (task<void> previousTask) { try{ previousTask.get(); //Get the result of task by forcing it. } catch(Exception^ ex) { OutputDebugStringW(L"exp = "); OutputDebugStringW(ex->Message->Data()); OutputDebugStringW(L"\n\n"); } });

    Till now, if you will get the pop-up window with exception, please see the following thread

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/de134da2-c8ae-4e60-847d-57642b4123c3


    • Edited by Mokarrom Hossain Tuesday, October 16, 2012 11:50 AM
    • Marked as answer by TonyChi Wednesday, October 17, 2012 6:33 AM
    Tuesday, October 16, 2012 11:35 AM
  • Thank you very much.

    :)

    Wednesday, October 17, 2012 6:34 AM
  •   

    GetFileTask.then([this](StorageFile^ file) { try{ OutputDebugStringW(L"Path : "); OutputDebugStringW( file->Path->Data()); OutputDebugStringW(L"\n\n"); } catch(Exception^ ex) { OutputDebugStringW(L"ex = "); OutputDebugStringW(ex->Message->Data()); OutputDebugStringW(L"\n\n"); }

    The try/catch blocks here are unneeded. when you chain the task continuations all exceptions will appear in the last continuation. you only need the try/catch there.
    Thursday, October 18, 2012 6:27 AM
  • @Chris Guzak,

    I know, try/catch is not necessary here. I am not used it. It comes from TonyChi's code by legacy. I keep it intentionally, so that TonyChi can understand his root problem easily. Because everybody feel comfortable in his own code.

    Any way, thanks for your clarification.


    Thursday, October 18, 2012 8:29 AM