locked
Why can't catch FileOpenPicker->PickSingleFileAsync unhandled exception? RRS feed

  • Question

  • Hi all,

    I can't catch the exception when cancel theFileOpenPicker->PickSingleFileAsync operation.

    FileOpenPicker^ picker = ref new FileOpenPicker();	
    picker->FileTypeFilter->Append(".mp4");
    picker->SuggestedStartLocation = PickerLocationId::VideosLibrary;
    
    task<StorageFile^> m_pickFileTask(picker->PickSingleFileAsync());
    m_pickFileTask.then([this](StorageFile^ files)
    {
    	try
    	{
    		x_tb1->Text = "bn3 click" + files->DisplayName->ToString();	
    	}
    	catch(Platform::Exception^ e)
    	{
    		x_tb1->Text = "bn3 exception";	
    	}
    	catch(...)
    	{
    		x_tb1->Text = "bn3 all exception";	
    	}
    });

    In general,
    The x_tb1->Text will same as the file name when I selected by FileOpenPicker

    But,
    When I doesn't select anyone, and cancel the operation.
    The parameter "files" should be null pointer.
    So, I want using try-catch to avoid this situation.

    The actual result is show a unhandled exception dialog.
    It means the try-catch doesn't catch this exception.
    Why?
    Is there any mistake for my code?

    Thanks

    Monday, May 21, 2012 9:56 AM

Answers

  • Hello,

    You can try these code to catch the error when you get the StorageFile from  PickSingleFileAsync()

    task<StorageFile^> m_pickFileTask(picker->PickSingleFileAsync());
    m_pickFileTask.then([this](task<void> t)
    {
    	try
    	{
    	       StorageFile^ files=safe_case<StorageFile^>(t.get());
    	}
    	catch(Platform::Exception^ e)
    	{
    		x_tb1->Text = "bn3 exception";	
    	}
    	catch(...)
    	{
    		x_tb1->Text = "bn3 all exception";	
    	}
    });

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Friday, June 1, 2012 8:18 AM
    Tuesday, May 22, 2012 5:53 AM

All replies

  • I'd do a nullptr check instead of relying on exception handling for this case.

    http://www.iconstructions.be

    Monday, May 21, 2012 2:25 PM
  • Hello,

    You can try these code to catch the error when you get the StorageFile from  PickSingleFileAsync()

    task<StorageFile^> m_pickFileTask(picker->PickSingleFileAsync());
    m_pickFileTask.then([this](task<void> t)
    {
    	try
    	{
    	       StorageFile^ files=safe_case<StorageFile^>(t.get());
    	}
    	catch(Platform::Exception^ e)
    	{
    		x_tb1->Text = "bn3 exception";	
    	}
    	catch(...)
    	{
    		x_tb1->Text = "bn3 all exception";	
    	}
    });

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Friday, June 1, 2012 8:18 AM
    Tuesday, May 22, 2012 5:53 AM