【问题标题】:concurrency and exception handling并发和异常处理
【发布时间】:2018-03-23 14:08:47
【问题描述】:

所以我有以下代码:

        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 语句,但在调试时似乎没有任何效果。应用程序将中断并显示未处理的异常错误。

【问题讨论】:

标签: exception concurrency exception-handling uwp c++-cli


【解决方案1】:

我记得你能够捕捉到这样的异常:

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)
    { }
});

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多