Exception Handling
This chapters discusses how to handle exceptions from a submitted taskflow so you can properly catch or propagate exceptions in your workload.
Catch an Exception from a Running Taskflow
When a task throws an exception, the executor will store that exception in the shared state referenced by the tf::get
method:
tf::Executor executor; tf::Taskflow taskflow; taskflow.emplace([](){ throw std::runtime_error("exception"); }); try { executor.run(taskflow).get(); } catch(const std::runtime_error& e) { std::cerr << e.what() << std::endl; }
An exception will automatically cancel the execution of its parent taskflow. All the subsequent tasks that have dependencies on that exception task will not run. For instance, the following code defines two tasks, A
and B
, where B
runs after A
. When A
throws an exception, the executor will cancel the execution of the taskflow, stopping every tasks that run after A
. In this case, B
will not run.
tf::Executor executor; tf::Taskflow taskflow; tf::Task A = taskflow.emplace([](){ throw std::runtime_error("exception on A"); }); tf::Task B = taskflow.emplace([](){ std::cout << "Task B\n"; }); A.precede(B); try { executor.run(taskflow).get(); } catch(const std::runtime_error& e) { std::cerr << e.what() << std::endl; }
~$ exception on A # execution of taskflow is cancelled after an execution is thrown
When multiple tasks throw exceptions simultaneously, the executor will only catch one exception and store it in the shared state. Other exceptions will be silently ignored. For example, the following taskflow may concurrently throw two exceptions from task B
and task C
. Only one exception, either B
or C
, will be propagated.
tf::Executor executor; tf::Taskflow taskflow; auto [A, B, C, D] = taskflow.emplace( []() { std::cout << "TaskA\n"; }, []() { std::cout << "TaskB\n"; throw std::runtime_error("Exception on Task B"); }, []() { std::cout << "TaskC\n"; throw std::runtime_error("Exception on Task C"); }, []() { std::cout << "TaskD will not be printed due to exception\n"; } ); A.precede(B, C); // A runs before B and C D.succeed(B, C); // D runs after B and C try { executor.run(taskflow).get(); } catch(const std::runtime_error& e) { // caught either B's or C's exception std::cout << e.what() << std::endl; }
Catch an Exception from a Subflow
When you join a subflow using tf::A
of the subflow sf
:
tf::Executor executor; tf::Taskflow taskflow; taskflow.emplace([](tf::Subflow& sf) { tf::Task A = sf.emplace([]() { std::cout << "Task A\n"; throw std::runtime_error("exception on A"); }); tf::Task B = sf.emplace([]() { std::cout << "Task B\n"; }); A.precede(B); // catch the exception try { sf.join(); } catch(const std::runtime_error& re) { std::cout << "exception thrown during subflow joining: " << re.what() << '\n'; } }); executor.run(taskflow).get();
When an exception is thrown, it will cancel the execution of the parent subflow. All the subsequent tasks that depend on that exception task will not run. The above code example has the following output:
Task A exception thrown during subflow joining: exception on A
Uncaught exception will be propagated to the parent level until being explicitly caught. For example, the code below will propagate the exception to the parent of the subflow, which in this case in its taskflow.
tf::Executor executor; tf::Taskflow taskflow; taskflow.emplace([](tf::Subflow& sf) { tf::Task A = sf.emplace([]() { std::cout << "Task A\n"; throw std::runtime_error("exception on A"); }); tf::Task B = sf.emplace([]() { std::cout << "Task B\n"; }); A.precede(B); // uncaught exception will propagate to the parent sf.join(); }); try { executor.run(taskflow).get(); } catch (const std::runtime_error& re) { std::cout << "exception thrown from running the taskflow: " << re.what() << '\n'; }
Task A exception thrown from running the taskflow: exception on A
Catch an Exception from an Async Task
Similar to std::
tf::Executor executor; auto fu = executor.async([](){ throw std::runtime_error("exception"); }); try { fu.get(); } catch(const std::runtime_error& e) { std::cerr << e.what() << std::endl; }
Running the program will show the exception message on the async task:
~$ exception
On the other hand, since tf::
tf::Taskflow taskflow; tf::Executor executor; // exception will be silently ignored executor.silent_async([](){ throw std::runtime_error("exception"); }); // exception will be propagated to the parent tf::Runtime task and then its Taskflow taskflow.emplace([&](tf::Runtime& rt){ rt.silent_async([](){ throw std::runtime_error("exception"); }); }); try { taskflow.get(); } catch(const std::runtime_error& re) { std::cout << re.what() << std::endl; }
Catch an Exception from a Corun Loop
When you corun a graph via tf::taskflow1
:
tf::Executor executor; tf::Taskflow taskflow1; tf::Taskflow taskflow2; taskflow1.emplace([](){ throw std::runtime_error("exception"); }); taskflow2.emplace([&](){ try { executor.corun(taskflow1); } catch(const std::runtime_error& re) { std::cout << re.what() << std::endl; } }); executor.run(taskflow2).get();
We can observe the same behavior when using tf::
tf::Executor executor; tf::Taskflow taskflow1; tf::Taskflow taskflow2; taskflow1.emplace([](){ throw std::runtime_error("exception"); }); taskflow2.emplace([&](tf::Runtime& rt){ try { rt.corun(taskflow1); } catch(const std::runtime_error& re) { std::cout << re.what() << std::endl; } }); executor.run(taskflow2).get();
For the above example, if the exception is not caught with tf::rt
in this case. Then, the exception will be propagated to taskflow2
.
tf::Executor executor; tf::Taskflow taskflow1; tf::Taskflow taskflow2; taskflow1.emplace([](){ throw std::runtime_error("exception"); }); taskflow2.emplace([&](tf::Runtime& rt){ rt.corun(taskflow1); }); try { executor.run(taskflow2).get(); } catch(const std::runtime_error& re) { std::cout << re.what() << std::endl; }
For the above example, if the exception is not caught with tf::rt
in this case. Then, the exception will be propagated to taskflow2
.