From 98d51e42c81c0674bc724eccbdf8521d9317998a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 11 Nov 2010 09:59:53 +0100 Subject: Support exception handling in QtConcurrent::run() Use QFutureInterface::reportException() in the same way that we do in QtConcurrent::map(). --- src/corelib/concurrent/qtconcurrentrunbase.h | 26 +++++++++++++-- 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 a6bbe3e..a7a5cc4 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::reportException(e); + } catch (...) { + QFutureInterface::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::reportException(e); + } catch (...) { + QFutureInterface::reportException(QtConcurrent::UnhandledException()); + } +#endif this->reportFinished(); } }; diff --git a/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/qtconcurrentrun/tst_qtconcurrentrun.cpp index 8fdc50c..8b10ea4 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() { -- cgit v0.12