According to MSDN,
A task-based continuation is always scheduled for execution when the antecedent task finishes, even when the antecedent task is canceled or throws an exception.
I don't understand this because I tried out the following code and the subsequent task isn't called when the first task has finished by throwing an exception. I understand why it must forward the call to the site where concurrency::task::wait is called but I don't understand what the statement on MSDN means. What am I misunderstanding?
#include <iostream>
#include <ppl.h>
#include <ppltasks.h>
int main(int argc, char* argv[])
{
using namespace std;
concurrency::task<void> task;
auto task_ticket = concurrency::create_task([]()
{
// A continuation task executed asynchronously
// after the previous task has completed. This
// is executed even if the previous task fails
// by being cancelled or throwing an exception.
throw std::runtime_error("Hello");
})
.then([]()
{
// This should be executed even though the
// previous task failed.
cout << "Task (2) executed" << endl;
});
try
{
task_ticket.wait();
}
catch (std::exception const& e)
{
cout << "Exception caught\n";
}
return EXIT_SUCCESS;
}
You're misunderstanding Value-Based Versus Task-Based Continuations.
Your initial call to create_task returns
task<void>. The lambda you pass to.thenacceptsvoidas input (since.then([]()is equivalent to.then([](void)), therefore the continuation is value-based and does not run if the antecedent task throws.To declare a task-based continuation, use: