ExceptionHandling

# Exception Handling

Catch an Exception from a Running Taskflow

Catch an Exception from a Running Taskflow

ExceptionHandling_1CatchAnExceptionFromARunningTaskflow
Catch an Exception from a Subflow

Catch an Exception from a Subflow

ExceptionHandling_1CatchAnExceptionFromASubflow
Catch an Exception from an Async Task

Catch an Exception from an Async Task

ExceptionHandling_1CatchAnExceptionFromAnAsyncTask
Catch an Exception from a Corun Loop

Catch an Exception from a Corun Loop

ExceptionHandling_1CatchAnExceptionFromACorunLoop

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::Future handle. You can catch that exception via calling the `get` method:

    tf::Executorexecutor;
    tf::Taskflowtaskflow;

    taskflow.emplace([](){throwstd::runtime_error("exception");});

    try{
    executor.run(taskflow).get();
    }
    catch(conststd::runtime_error&e){
    std::cerr<<e.what()<<std::endl;
    }

As tf::Future is derived from std::future, it inherits all the exception handling behaviors defined by the C++ standard.

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::Executorexecutor;
    tf::Taskflowtaskflow;

    tf::TaskA=taskflow.emplace([](){throwstd::runtime_error("exceptiononA");});
    tf::TaskB=taskflow.emplace([](){std::cout<<"TaskB\n";});
    A.precede(B);

    try{
    executor.run(taskflow).get();
    }
    catch(conststd::runtime_error&e){
    std::cerr<<e.what()<<std::endl;
    }

    {.shell-session}
    ~$exceptiononA
    #executionoftaskflowiscancelledafteranexecutionisthrown

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::Executorexecutor;
    tf::Taskflowtaskflow;

    auto[A,B,C,D]=taskflow.emplace(
    [](){std::cout<<"TaskA\n";},
    [](){
    std::cout<<"TaskB\n";
    throwstd::runtime_error("ExceptiononTaskB");
    },
    [](){
    std::cout<<"TaskC\n";
    throwstd::runtime_error("ExceptiononTaskC");
    },
    [](){std::cout<<"TaskDwillnotbeprintedduetoexception\n";}
    );

    A.precede(B,C);//ArunsbeforeBandC
    D.succeed(B,C);//DrunsafterBandC

    try{
    executor.run(taskflow).get();
    }
    catch(conststd::runtime_error&e){
    //caughteitherB'sorC'sexception
    std::cout<<e.what()<<std::endl;
    }

# Catch an Exception from a Subflow

When you join a subflow using tf::Subflow::join, you can catch an exception thrown by its children tasks. For example, the following code catches an exception from the child task `A` of the subflow `sf`:

    tf::Executorexecutor;
    tf::Taskflowtaskflow;

    taskflow.emplace([](tf::Subflow&sf){
    tf::TaskA=sf.emplace([](){
    std::cout<<"TaskA\n";
    throwstd::runtime_error("exceptiononA");
    });
    tf::TaskB=sf.emplace([](){
    std::cout<<"TaskB\n";
    });
    A.precede(B);

    //catchtheexception
    try{
    sf.join();
    }
    catch(conststd::runtime_error&re){
    std::cout<<"exceptionthrownduringsubflowjoining:"<<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:

    {.shell-session}
    TaskA
    exceptionthrownduringsubflowjoining:exceptiononA

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::Executorexecutor;
    tf::Taskflowtaskflow;

    taskflow.emplace([](tf::Subflow&sf){
    tf::TaskA=sf.emplace([](){
    std::cout<<"TaskA\n";
    throwstd::runtime_error("exceptiononA");
    });
    tf::TaskB=sf.emplace([](){
    std::cout<<"TaskB\n";
    });
    A.precede(B);

    //uncaughtexceptionwillpropagatetotheparent
    sf.join();
    });

    try
    {
    executor.run(taskflow).get();
    }
    catch(conststd::runtime_error&re)
    {
    std::cout<<"exceptionthrownfromrunningthetaskflow:"<<re.what()<<'\n';
    }

    {.shell-session}
    TaskA
    exceptionthrownfromrunningthetaskflow:exceptiononA

# Catch an Exception from an Async Task

Similar to std::future, tf::Executor::async will store the exception in the shared state referenced by the returned std::future handle.

    tf::Executorexecutor;
    autofu=executor.async([](){throwstd::runtime_error("exception");});
    try{
    fu.get();
    }
    catch(conststd::runtime_error&e){
    std::cerr<<e.what()<<std::endl;
    }

Running the program will show the exception message on the async task:

    {.shell-session}
    ~$exception

On the other hand, since tf::Executor::silent_async 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.

    tf::Taskflowtaskflow;
    tf::Executorexecutor;

    //exceptionwillbesilentlyignored
    executor.silent_async([](){throwstd::runtime_error("exception");});

    //exceptionwillbepropagatedtotheparenttf::RuntimetaskandthenitsTaskflow
    taskflow.emplace([&](tf::Runtime&rt){
    rt.silent_async([](){throwstd::runtime_error("exception");});
    });
    try{
    taskflow.get();
    }
    catch(conststd::runtime_error&re){
    std::cout<<re.what()<<std::endl;
    }

# Catch an Exception from a Corun Loop

When you corun a graph via tf::Executor::corun or tf::Runtime::corun, any exception will be thrown during the execution. For example, the code below will throw an exception during the execution of `taskflow1`:

    tf::Executorexecutor;
    tf::Taskflowtaskflow1;
    tf::Taskflowtaskflow2;

    taskflow1.emplace([](){
    throwstd::runtime_error("exception");
    });
    taskflow2.emplace([&](){
    try{
    executor.corun(taskflow1);
    }catch(conststd::runtime_error&re){
    std::cout<<re.what()<<std::endl;
    }
    });
    executor.run(taskflow2).get();

We can observe the same behavior when using tf::Runtime::corun:

    tf::Executorexecutor;
    tf::Taskflowtaskflow1;
    tf::Taskflowtaskflow2;

    taskflow1.emplace([](){
    throwstd::runtime_error("exception");
    });
    taskflow2.emplace([&](tf::Runtime&rt){
    try{
    rt.corun(taskflow1);
    }catch(conststd::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::Runtime::corun, it will be propagated to its parent task, which is the tf::Runtime object `rt` in this case. Then, the exception will be propagated to `taskflow2`.

    tf::Executorexecutor;
    tf::Taskflowtaskflow1;
    tf::Taskflowtaskflow2;

    taskflow1.emplace([](){
    throwstd::runtime_error("exception");
    });
    taskflow2.emplace([&](tf::Runtime&rt){
    rt.corun(taskflow1);
    });

    try{
    executor.run(taskflow2).get();
    }
    catch(conststd::runtime_error&re){
    std::cout<<re.what()<<std::endl;
    }

For the above example, if the exception is not caught with tf::Runtime::corun, it will be propagated to its parent task, which is the tf::Runtime object `rt` in this case. Then, the exception will be propagated to `taskflow2`.