Exception 并发和异常处理

Exception 并发和异常处理,exception,concurrency,exception-handling,uwp,c++-cli,Exception,Concurrency,Exception Handling,Uwp,C++ Cli,因此,我有以下代码: StorageFolder^ storageFolder = ApplicationData::Current->LocalFolder; concurrency::create_task(storageFolder->GetFileAsync(txtfile)).then([](StorageFile^ sampleFile) { concurrency::create_task(Fi

因此,我有以下代码:

        StorageFolder^ storageFolder = ApplicationData::Current->LocalFolder;

        concurrency::create_task(storageFolder->GetFileAsync(txtfile)).then([](StorageFile^ sampleFile)
        {

            concurrency::create_task(FileIO::AppendTextAsync(sampleFile, New_Program_Data)).then([]() {

            });

        });
我用它打开一个文本文件,并在末尾附加一些数据。如果文件丢失、损坏或无法打开,如何添加异常处理


我在任务内外尝试了各种try-and-catch语句,但调试时似乎没有任何效果。应用程序将中断并显示未处理的异常错误。

我记得您可以捕获如下异常:

concurrency::create_task(storageFolder->GetFileAsync(txtfile))
.then([](StorageFile^ sampleFile)
{
    // Do whatever you want when successful.
})
.then([] (task<void> previousTask)
{
    // Catch any exceptions of your previous task here.

    try
    {
        // Get the result of the previous task.
        // This also results in exceptions getting thrown.
        previousTask.get();
    }
    catch(Exception^ ex)
    { }
});
concurrency::创建_任务(storageFolder->GetFileAsync(txtfile))
。然后([](存储文件^sampleFile)
{
//当你成功的时候,你想做什么就做什么。
})
。然后([](任务之前的任务)
{
//在此处捕获上一个任务的任何异常。
尝试
{
//获取上一个任务的结果。
//这也会导致抛出异常。
previousTask.get();
}
捕获(异常^ex)
{ }
});
您需要使用
task
s作为计数函数的参数来获取异常,如果使用
StorageFile^
您将无法获取异常。看到和