Search code examples
c++exceptionparallel-processingtry-catchppl

c++ catch error raised by PPL parallel_for


I have written this piece of code to catch error launched by ppl

    try
    {
        parallel_for (m_row_start, m_row_end + 1, [&functionEvaluation,varModel_,this](int i)
        {
             // do things
        });
    }
    catch(const std::exception error_)
    {
        QString t(error_.what());
    }



    try
    {
        return functionEvaluation.combine(plus<double>());
    }
    catch(const std::exception error_)
    {
        QString t(error_.what());
    }

No error is caught although I have strong suspicion that it does have exception raised (a larger try{}catch(...){} it catching an std::exception, with no clear message.

I am right with my syntax for catching exception raised in ppl code?


Solution

  • Your syntax is correct although there's no reason you couldn't catch by reference to avoid unnecessary copying of the exception object:

     catch(const std::exception & error_)
    
    1. Check that the exception thrown actually derives from std::exception.
    2. The PPL will only allow exceptions to propagate once all the threads have completed, could you have a thread which is still running preventing you from seeing the exception?

    For debugging purposes, you could add an extra catch block:

    catch(...)
    {
      cout << "Unknown exception" << endl;
    }
    

    Just to check if you are getting any kind of exception thrown, however I wouldn't leave this in production code because there's no way to usefully do anything with the exception.