5

我知道实际处理在不同线程中引发的异常是没有意义的,但是有什么方法可以通知我至少发生了一个异常?例如像

#include <QtConcurrentRun>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

int main()
{
  try
  {
    QtConcurrent::run(MyFunction);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
  }

}

即使发生异常,也会安静地退出。当异常来自调用堆栈深处的某个地方时,这有时会非常令人困惑。

- - - - - - 编辑 - - - - - - -

我试图写一个像 UmNyobe 建议的包装器,但我一定是函数指针有问题?

#include <QtConcurrentRun>
#include <QFutureWatcher>
#include <QObject>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

template<typename TFirstParam, typename... TParams>
bool ExceptionWrapper(TFirstParam firstParam, TParams&& ...params)
{
  // Here 'firstParam' should be a function pointer, and 'params' are the arguments
  // that should be passed to the function
  try
  {
    firstParam(params...);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
    return false; // failure
  }

  return true; // success
}

struct MyClass : public QObject
{
  Q_OBJECT

  MyClass()
  {
    connect(&this->FutureWatcher, SIGNAL(finished()), this, SLOT(slot_finished()));
  }

  void DoSomething()
  {
    void (*myFunctionPointer)() = MyFunction;
    bool (*functionPointer)(decltype(myFunctionPointer)) = ExceptionWrapper;

    QFuture<bool> future = QtConcurrent::run(functionPointer);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

  void slot_finished()
  {
    std::cout << "Finished" << std::endl;
    if(!this->FutureWatcher.result())
    {
      std::cout << "There was an error!" << std::endl;
    }
  }
};

#include "ExceptionWrapper.moc"

int main()
{
  MyClass myClass = new MyClass;
  myClass->DoSomething();
}

我得到的错误是在这一行:

QFuture<bool> future = QtConcurrent::run(functionPointer);

error: no matching function for call to 'run(bool (*&)(void (*)()))'
4

3 回答 3

8

我知道实际处理在不同线程中引发的异常是没有意义的,但是有什么方法可以通知我至少发生了一个异常?

您可以使用从QtConcurrent::run. 有关详细信息,请参阅此页面。当您收集未来时,将重新抛出任何未处理的异常。您可以创建一个简单的包装类来捕获异常并在接收线程中检查它。

#include <QtGui>
#include <iostream>
#include <stdexcept>

class MyException : public QtConcurrent::Exception
{
public:
    MyException(std::exception& err) : e(err) {}
    void raise() const { throw *this; }
    Exception* clone() const { return new MyException(*this); }
    std::exception error() const { return e; }
private:
    std::exception e;
};

// first concurrent function
int addFive(int n)
{
    try
    {
        throw std::runtime_error("kablammo!");
        //throw -1;
        return n + 5;
    }
    catch (std::exception& e)
    {
        throw MyException(e);
    }

}

// second concurrent function    
void myVoidFunction()
{
    try
    {
        throw std::runtime_error("oops!");
        //throw -1;
    }
    catch (std::exception& e)
    {
        throw MyException(e);
    }
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QFuture<int> f1 = QtConcurrent::run(addFive, 50);
    try
    {
        int r = f1.result();
        std::cout << "result = " << r << std::endl;
    }
    catch (MyException& me)
    {
        std::cout << me.error().what() << std::endl;
    }
    catch (QtConcurrent::UnhandledException&)
    {
        std::cout << "unhandled exception in addFive\n";
    }

    QFuture<void> f2 = QtConcurrent::run(myVoidFunction);
    try
    {
        // result() not available for QFuture<void>, use waitForFinished() to
        // block until it's done.
        f2.waitForFinished();
        std::cout << "myVoidFunction finished\n";
    }
    catch (MyException& me)
    {
        std::cout << me.error().what() << std::endl;
    }
    catch (QtConcurrent::UnhandledException&)
    {
        std::cout << "unhandled exception in myVoidFunction\n";
    }

    QWidget w;
    w.show();

    return app.exec();
}
于 2012-12-05T20:44:15.200 回答
2

似乎如果抛出异常,关联的 QFutureWatcher 的 isCanceled() 返回 true:

#include <QApplication>
#include <QtConcurrentRun>
#include <QFutureWatcher>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

struct MyClass : public QObject
{
  Q_OBJECT

public:
  MyClass()
  {
    connect(&this->FutureWatcher, SIGNAL(finished()), this, SLOT(slot_finished()));
  }

  void DoSomething()
  {
    QFuture<void> future = QtConcurrent::run(MyFunction);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

public slots:
  void slot_finished()
  {
    std::cout << "Finished" << std::endl;
    if(this->FutureWatcher.isCanceled())
    {
      std::cout << "There was an error!" << std::endl;
    }
    else
    {
      std::cout << "Success!" << std::endl;
    }
  }
};

#include "Exception.moc"

int main(int argc, char*argv[])
{
  MyClass myClass;
  myClass.DoSomething();

  QApplication app(argc, argv);

  return app.exec();
}

--------- 编辑(戈登弗里曼答案的简化版) ---------

即使不使用 QtConcurrent::Exception 子类,似乎也会重新抛出异常?

#include <QtGui>
#include <iostream>
#include <stdexcept>

// non-void concurrent function
int addFive(int n)
{
  throw std::runtime_error("addFive throw!");
  return n+5;
}

// void concurrent function
void myVoidFunction()
{
   throw std::runtime_error("myVoidFunction throw!");
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QFuture<int> f1 = QtConcurrent::run(addFive, 50);
    try
    {
        int r = f1.result();
        std::cout << "result = " << r << std::endl;
    }
    catch (...)
    {
      std::cout << "exception in addFive." << std::endl;
    }

    QFuture<void> f2 = QtConcurrent::run(myVoidFunction);
    try
    {
        // result() not available for QFuture<void>, use waitForFinished() to
        // block until it's done.
        f2.waitForFinished();
        std::cout << "myVoidFunction finished\n";
    }
    catch (...)
    {
       std::cout << "exception in myVoidFunction\n";
    }

    QWidget w;
    w.show();

    return app.exec();
}
于 2012-12-05T18:46:11.560 回答
0

好处QtConcurrent::run 是它接受带有返回值的函数。我的两分钱:尽早捕获异常......

免责声明:C++ 中的例外情况我真的很糟糕:D

在不同线程中调用的代码应该返回一个值。对于 void 函数或不捕获异常的现有函数,您可以定义一个包装器(通用或非通用)。例如

 int exceptionwrapper(){
   int exception  = 0;
   try
   {
      myFunction();
   }
   catch(...){
      exception = 1;
      std::cout << "Exception caught!" << std::endl;
   }
   return exception;
 }

然后稍后

 QFuture<int> future = QtConcurrent::run(exemptionwrapper);
 futurewatcher.setFuture(future);

当函数结束时,您只需使用未来观察者就可以在以后检查未来。

于 2012-12-05T16:29:27.040 回答