How does one catch I/O exceptions on windows phone 8, c++/cx?
EDIT: Here’s a full example
Checking to see if a file “hello.txt” exist:
StorageFile^ Testme(String^ fileName)
{
StorageFolder^ item = ApplicationData::Current->LocalFolder;
try
{
task<StorageFile^> getFileTask(item->GetFileAsync("hello.txt"));
getFileTask.then([](StorageFile^ storageFile)
{
return storageFile;
});
}
catch (Exception^ ex)
{
OutputDebugString(L"Caught the exception");
}
return nullptr;
}
If “hello.txt” exsit, method Testme returns the file ptr like a charm.
If “hello.txt does not exist, not only does it not throw exception FileNOtFound, instead I go crashing with this showing up in the debugger window:
Unhandled exception at 0x71D49C01 (Msvcr110d.dll) in MyPhoneApp.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
If there is a handler for this exception, the program may be safely continued
What’s wrong and how would I elegantly check if a file exist’s or not in WP8?
I really hope someone answers … Thanks.
After spending hours to figure out what the problem was in a related issue, I finally figured it out. Using C++, it looks Visual Studio behaves a little interesting. Instead of passing the exception to the user, it just throws it. That means even if you had a handler for the exception, your handler was not allowed to handle it. I should note here that this only happens when you run your App inside Visual Studio. Deploying and launching app shows no problem.
So to resolve it, open the exceptions settings (from Menu > Debug > Exceptions – or Ctrl+D, E). Enlarge “C++ Exceptions” and deselect “Platform::InvalidArgumentException” in “Thrown” column. Then you should be good to go.
Update after first comment:
First of all, I had to deselect COMException from the list too for below example to work.
In addition to doing above. It is important to understand async programming in C++/CX. You can not simply return from the function after creating the task. If you really need to return you need to return the worker task you created to do the job. Below is a Windows Store app sample (not WP) but they should work the same. Your helper function must be like below.
And you should call it like below.
I do not know where local folder is so I could not test the condition in which the file is actually there. Please test and see.