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?

有帮助吗?

解决方案

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.

其他提示

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;)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top