<li><ahref="#CatchAnExceptionFromARunningTaskflow">Catch an Exception from a Running Taskflow</a></li>
<li><ahref="#CatchAnExceptionFromASubflow">Catch an Exception from a Subflow</a></li>
<li><ahref="#CatchAnExceptionFromAnAsyncTask">Catch an Exception from an Async Task</a></li>
<li><ahref="#CatchAnExceptionFromACorunLoop">Catch an Exception from a Corun Loop</a></li>
</ul>
</nav>
<p>This chapters discusses how to handle exceptions from a submitted taskflow so you can properly catch or propagate exceptions in your workload.</p><sectionid="CatchAnExceptionFromARunningTaskflow"><h2><ahref="#CatchAnExceptionFromARunningTaskflow">Catch an Exception from a Running Taskflow</a></h2><p>When a task throws an exception, the executor will store that exception in the shared state referenced by the <ahref="classtf_1_1Future.html" class="m-doc">tf::<wbr/>Future</a> handle. You can catch that exception via calling the <code>get</code> method:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="p">}</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>As <ahref="classtf_1_1Future.html" class="m-doc">tf::<wbr/>Future</a> is derived from <ahref="https://en.cppreference.com/w/cpp/thread/future">std::<wbr/>future</a>, it inherits all the exception handling behaviors defined by the C++ standard.</p></aside><p>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, <code>A</code> and <code>B</code>, where <code>B</code> runs after <code>A</code>. When <code>A</code> throws an exception, the executor will cancel the execution of the taskflow, stopping every tasks that run after <code>A</code>. In this case, <code>B</code> will not run.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Task</span><spanclass="w"></span><spanclass="n">A</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">.</span><spanclass="n">emplace</span><spanclass="p">([](){</span><spanclass="w"></span><spanclass="k">throw</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">runtime_error</span><spanclass="p">(</span><spanclass="s">"exception on A"</span><spanclass="p">);</span><spanclass="w"></span><spanclass="p">});</span>
<spanclass="p">}</span></pre><preclass="m-console"><spanclass="go">~$ exception on A</span>
<spanclass="gp"># </span>execution<spanclass="w"></span>of<spanclass="w"></span>taskflow<spanclass="w"></span>is<spanclass="w"></span>cancelled<spanclass="w"></span>after<spanclass="w"></span>an<spanclass="w"></span>execution<spanclass="w"></span>is<spanclass="w"></span>thrown</pre><p>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 <code>B</code> and task <code>C</code>. Only one exception, either <code>B</code> or <code>C</code>, will be propagated.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="k">throw</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">runtime_error</span><spanclass="p">(</span><spanclass="s">"Exception on Task B"</span><spanclass="p">);</span>
<spanclass="w"></span><spanclass="k">throw</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">runtime_error</span><spanclass="p">(</span><spanclass="s">"Exception on Task C"</span><spanclass="p">);</span>
<spanclass="w"></span><spanclass="p">},</span>
<spanclass="w"></span><spanclass="p">[]()</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"TaskD will not be printed due to exception</span><spanclass="se">\n</span><spanclass="s">"</span><spanclass="p">;</span><spanclass="w"></span><spanclass="p">}</span>
<spanclass="p">);</span>
<spanclass="n">A</span><spanclass="p">.</span><spanclass="n">precede</span><spanclass="p">(</span><spanclass="n">B</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">C</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// A runs before B and C</span>
<spanclass="n">D</span><spanclass="p">.</span><spanclass="n">succeed</span><spanclass="p">(</span><spanclass="n">B</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">C</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// D runs after B and C</span>
<spanclass="p">}</span></pre></section><sectionid="CatchAnExceptionFromASubflow"><h2><ahref="#CatchAnExceptionFromASubflow">Catch an Exception from a Subflow</a></h2><p>When you join a subflow using <ahref="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245" class="m-doc">tf::<wbr/>Subflow::<wbr/>join</a>, you can catch an exception thrown by its children tasks. For example, the following code catches an exception from the child task <code>A</code> of the subflow <code>sf</code>:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="k">throw</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">runtime_error</span><spanclass="p">(</span><spanclass="s">"exception on A"</span><spanclass="p">);</span><spanclass="w"></span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"exception thrown during subflow joining: "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">re</span><spanclass="p">.</span><spanclass="n">what</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="p">}</span>
<spanclass="p">});</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow</span><spanclass="p">).</span><spanclass="n">get</span><spanclass="p">();</span></pre><p>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:</p><preclass="m-console"><spanclass="go">Task A</span>
<spanclass="go">exception thrown during subflow joining: exception on A</span></pre><p>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.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="k">throw</span><spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">runtime_error</span><spanclass="p">(</span><spanclass="s">"exception on A"</span><spanclass="p">);</span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"exception thrown from running the taskflow: "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">re</span><spanclass="p">.</span><spanclass="n">what</span><spanclass="p">()</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="go">exception thrown from running the taskflow: exception on A</span></pre></section><sectionid="CatchAnExceptionFromAnAsyncTask"><h2><ahref="#CatchAnExceptionFromAnAsyncTask">Catch an Exception from an Async Task</a></h2><p>Similar to <ahref="https://en.cppreference.com/w/cpp/thread/future">std::<wbr/>future</a>, <ahref="classtf_1_1Executor.html#af960048056f7c6b5bc71f4f526f05df7" class="m-doc">tf::<wbr/>Executor::<wbr/>async</a> will store the exception in the shared state referenced by the returned <ahref="https://en.cppreference.com/w/cpp/thread/future">std::<wbr/>future</a> handle.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="p">}</span></pre><p>Running the program will show the exception message on the async task:</p><preclass="m-console"><spanclass="go">~$ exception</span></pre><p>On the other hand, since <ahref="classtf_1_1Executor.html#a0461cb2c459c9f9473c72af06af9c701" class="m-doc">tf::<wbr/>Executor::<wbr/>silent_async</a> does not return any future handle, any exception thrown from a silent-async task will be silently caught by the executor and (1) propagated to the its parent task if the parent task exists or (2) ignored if the parent task does not exist.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Taskflow</span><spanclass="w"></span><spanclass="n">taskflow</span><spanclass="p">;</span>
<spanclass="p">}</span></pre></section><sectionid="CatchAnExceptionFromACorunLoop"><h2><ahref="#CatchAnExceptionFromACorunLoop">Catch an Exception from a Corun Loop</a></h2><p>When you corun a graph via <ahref="classtf_1_1Executor.html#a8fcd9e0557922bb8194999f0cd433ea8" class="m-doc">tf::<wbr/>Executor::<wbr/>corun</a> or <ahref="classtf_1_1Runtime.html#a1c772e90614302024cfa52fa86d75cac" class="m-doc">tf::<wbr/>Runtime::<wbr/>corun</a>, any exception will be thrown during the execution. For example, the code below will throw an exception during the execution of <code>taskflow1</code>:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow2</span><spanclass="p">).</span><spanclass="n">get</span><spanclass="p">();</span></pre><p>We can observe the same behavior when using <ahref="classtf_1_1Runtime.html#a1c772e90614302024cfa52fa86d75cac" class="m-doc">tf::<wbr/>Runtime::<wbr/>corun</a>:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="n">executor</span><spanclass="p">.</span><spanclass="n">run</span><spanclass="p">(</span><spanclass="n">taskflow2</span><spanclass="p">).</span><spanclass="n">get</span><spanclass="p">();</span></pre><p>For the above example, if the exception is not caught with <ahref="classtf_1_1Runtime.html#a1c772e90614302024cfa52fa86d75cac" class="m-doc">tf::<wbr/>Runtime::<wbr/>corun</a>, it will be propagated to its parent task, which is the <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a> object <code>rt</code> in this case. Then, the exception will be propagated to <code>taskflow2</code>.</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">Executor</span><spanclass="w"></span><spanclass="n">executor</span><spanclass="p">;</span>
<spanclass="p">}</span></pre><p>For the above example, if the exception is not caught with <ahref="classtf_1_1Runtime.html#a1c772e90614302024cfa52fa86d75cac" class="m-doc">tf::<wbr/>Runtime::<wbr/>corun</a>, it will be propagated to its parent task, which is the <ahref="classtf_1_1Runtime.html" class="m-doc">tf::<wbr/>Runtime</a> object <code>rt</code> in this case. Then, the exception will be propagated to <code>taskflow2</code>.</p></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>