c++ - How to handle exceptions from StorageFile::OpenAsync when URI is bad -


i have section of code correctly load images http uris when uris valid cannot figure how catch exception openasync throws when uri invalid (results in 404).

the problem when lambda contains call openasync exits, exception thrown; exception not thrown while in try/catch block.

the question is:

what correct way catch exception thrown storagefile::openasync?

auto bm = ref new bitmapimage(); try {     uri^ uri = ref new uri("http://invaliduri.tumblr.com/avatar/128");      auto task = concurrency::create_task(createstreamedfilefromuriasync("temp-web-file.png", uri, nullptr));      task.then([] (storagefile^ file) {         try {             return file->openasync(fileaccessmode::read);         } catch (...) {             // not catch exception because exception             //   occurs after lambda exitted         }     }).then([bm](irandomaccessstream^ inputstream) {         try {             return bm->setsourceasync(inputstream);         } catch (...) {             // not catch exception because exception             //   occurs before lambda entered         }     }); } catch (...) {     // , not catch exception } 

i had question 3 years later. referenced this article. scenario was, then, solved follows,

#include<ppltasks.h> ... auto file = ref new windows::foundation::uri::uri("ms-appx:///somefile.txt"); concurrency::create_task(windows::storage::storagefile::getfilefromapplicationuriasync(data)) .then([](windows::storage::storagefile^ f) {     return windows::storage::fileio::readtextasync(f); }) .then([this](string^ s) {     this->somefilecontent = s; }) .then([](concurrency::task<void> t) {     try {         t.get();     } catch(platform::comexception^ e) {         outputdebugstring(e->message->data());     } }); 

this async task chain may fail in getfilefromapplicationuriasync or in readtextasync throwing exception. key when thrown matching then(...) prototype final one. on entering try block, task::get re-throws exception caught concurrency classes on behalf.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -