kiriloff
kiriloff

Reputation: 26333

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?

Upvotes: 2

Views: 478

Answers (2)

Benj
Benj

Reputation: 32398

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.

Upvotes: 3

quetzalcoatl
quetzalcoatl

Reputation: 33546

First, check what is thrown. If you mistype the catch, it will not react. Maybe it simply is the CONST marker? const-type is not the same as non-const-type, but I actually don't remember well if catches are const-volatile-sensitive.

Second, unless strong reasons arise, always catch by reference:

catch(std::exception& error)

If you do not, then an exception copying will occur: http://www.parashift.com/c++-faq/what-to-catch.html By copying I mean object-copying, not re-raising;)

Upvotes: 0

Related Questions