diff options
author | Morten Johan Sørvig <morten.sorvig@nokia.com> | 2010-11-11 08:59:53 (GMT) |
---|---|---|
committer | Qt Commercial Integration <QtCommercial@digia.com> | 2012-01-31 10:24:54 (GMT) |
commit | b06610a65229cd1cb5aa5d9d5145e976e99f631c (patch) | |
tree | 16d826dbb324f39d7e7aaefbb6d4704cb3ae38b0 | |
parent | e4b978f5745f8a05da93b5627be5557751fec773 (diff) | |
download | Qt-b06610a65229cd1cb5aa5d9d5145e976e99f631c.zip Qt-b06610a65229cd1cb5aa5d9d5145e976e99f631c.tar.gz Qt-b06610a65229cd1cb5aa5d9d5145e976e99f631c.tar.bz2 |
Support exception handling in QtConcurrent::run()
Use QFutureInterface::reportException() in the
same way that we do in QtConcurrent::map().
-rw-r--r-- | src/corelib/concurrent/qtconcurrentrunbase.h | 26 | ||||
-rw-r--r-- | tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp | 38 |
2 files changed, 62 insertions, 2 deletions
diff --git a/src/corelib/concurrent/qtconcurrentrunbase.h b/src/corelib/concurrent/qtconcurrentrunbase.h index c608af6..c9bd15e 100644 --- a/src/corelib/concurrent/qtconcurrentrunbase.h +++ b/src/corelib/concurrent/qtconcurrentrunbase.h @@ -100,7 +100,19 @@ public: this->reportFinished(); return; } - this->runFunctor(); +#ifndef QT_NO_EXCEPTIONS + try { +#endif + this->runFunctor(); +#ifndef QT_NO_EXCEPTIONS + } catch (QtConcurrent::Exception &e) { + qDebug() << "cought exception"; + QFutureInterface<T>::reportException(e); + } catch (...) { + QFutureInterface<T>::reportException(QtConcurrent::UnhandledException()); + } +#endif + this->reportResult(result); this->reportFinished(); } @@ -117,7 +129,17 @@ public: this->reportFinished(); return; } - this->runFunctor(); +#ifndef QT_NO_EXCEPTIONS + try { +#endif + this->runFunctor(); +#ifndef QT_NO_EXCEPTIONS + } catch (QtConcurrent::Exception &e) { + QFutureInterface<void>::reportException(e); + } catch (...) { + QFutureInterface<void>::reportException(QtConcurrent::UnhandledException()); + } +#endif this->reportFinished(); } }; diff --git a/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp index 77709a4..e4e9479 100644 --- a/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp +++ b/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp @@ -61,6 +61,9 @@ private slots: void implicitConvertibleTypes(); void runWaitLoop(); void recursive(); +#ifndef QT_NO_EXCEPTIONS + void exceptions(); +#endif #if 0 void createFunctor(); #endif @@ -374,6 +377,41 @@ int fn2(double, int *) return 1; } + +#ifndef QT_NO_EXCEPTIONS +void throwFunction() +{ + throw QtConcurrent::Exception(); +} + +int throwFunctionReturn() +{ + throw QtConcurrent::Exception(); + return 0; +} + +void tst_QtConcurrentRun::exceptions() +{ + bool caught = false; + try { + QtConcurrent::run(throwFunction).waitForFinished(); + } catch (Exception &e) { + caught = true; + } + if (!caught) + QFAIL("did not get exception"); + + caught = false; + try { + QtConcurrent::run(throwFunctionReturn).waitForFinished(); + } catch (Exception &e) { + caught = true; + } + if (!caught) + QFAIL("did not get exception"); +} +#endif + #if 0 void tst_QtConcurrentRun::createFunctor() { |