Hello,
I need to throw an exception across a thread boundary.
I thought this was possible with QtConcurrent::Exception after reading the docs (http://doc.trolltech.com/4.4/qtconcu...exception.html) however this does not seem to work for me. I tested the following code with gcc 4.3 and with VS2008:
#include <QtCore>
class MyException : public QtConcurrent::Exception
{
public:
void raise() const { throw *this; }
Exception *clone() const { return new MyException(*this); }
};
void runInOtherThread()
{
qDebug("other thread");
throw MyException();
}
int main(int argc, char* argv[])
{
try
{
QtConcurrent::run(&runInOtherThread);
}
catch(MyException &e)
{
qDebug("exception caught");
}
return a.exec();
}
#include <QtCore>
class MyException : public QtConcurrent::Exception
{
public:
void raise() const { throw *this; }
Exception *clone() const { return new MyException(*this); }
};
void runInOtherThread()
{
qDebug("other thread");
throw MyException();
}
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
try
{
QtConcurrent::run(&runInOtherThread);
}
catch(MyException &e)
{
qDebug("exception caught");
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I would expect to end up in the catch block in the main() function. However i get the following output:
$ ./exceptiontest
other thread
Qt Concurrent has caught an exception thrown from a worker thread.
This is not supported, exceptions thrown in worker threads must be
caught before control returns to Qt Concurrent.
terminate called after throwing an instance of 'MyException'
what(): std::exception
Aborted
$
$ ./exceptiontest
other thread
Qt Concurrent has caught an exception thrown from a worker thread.
This is not supported, exceptions thrown in worker threads must be
caught before control returns to Qt Concurrent.
terminate called after throwing an instance of 'MyException'
what(): std::exception
Aborted
$
To copy to clipboard, switch view to plain text mode
The warning is printed in QThreadPoolThread::run().
This conflicts with my interpretation of the manual, which says:
Qt Concurrent supports throwing and catching exceptions across thread boundaries, provided that the exception inherit from QtConcurrent::Exception and implement two helper functions:
Qt Concurrent supports throwing and catching exceptions across thread boundaries, provided that the exception inherit from QtConcurrent::Exception and implement two helper functions:
To copy to clipboard, switch view to plain text mode
But probably i'm just doing something wrong here 
Any help or pointers would be very much appreciated 
thanks,
glenn
Bookmarks