Fix Windows Store Exceptions from GetFileAsync

I have a problem with the following code example:

    Windows::Storage::StorageFolder^ location = Package::Current->InstalledLocation;

    try
    {
        task<StorageFile^> GetFileTask(location->GetFileAsync(sn));

        GetFileTask.then([=](StorageFile^ file)
        {
            try
            {
                task<IBuffer^> ReadFileTask(FileIO::ReadBufferAsync(file));

                ReadFileTask.then([=](IBuffer^ readBuffer)
                { 
                    // process file contents here      
                });
            }
            catch(Platform::Exception^ ex)
            {
                // Handle error here
            }

        });
    }
    catch(Platform::Exception^ ex)
    {
        // Handle error here
    }

When using a file name that does not exist, the function throws an exception:

Unhandled exception in 0x0FFCC531 (msvcr110d.dll) in GameTest2.exe: An invalid parameter was passed to a function that considers invalid parameters to be fatal.

I searched the Internet, and this exception breaks only when connected to the debugger. I am using VS 2012. I have disabled all relevant "break on exception", but it still calls the debugger, which is interrupted, and not my handlers get the opportunity to handle the exception.

If the file is missing, I would expect the GetFileAsync method to throw a "File does not exist" exception. Not sure why it keeps throwing an "Invalid parameter" exception.

, - . - ?

, < > . GetFileAsync, 'wait'. , "" , GetFileAsync, .

, .

Update:

, :

    task<StorageFile^>( location->GetFileAsync(sn)).then([](StorageFile^ openedFile)
    {
        return FileIO::ReadBufferAsync(openedFile);
    }).then([](IBuffer^ readBuffer)
    {
        // Process file

    }).then([](task<void> t)
    {
        try
        {
            t.get();
        }
        catch(Platform::Exception^ e)
        {
            // Handle error
        }
    });

, "then", .

+4

Source: https://habr.com/ru/post/1534529/


All Articles