[Qt-interest] Throwing C++ exception from a wrapped Qobject in QScript
Liebe Markus (RtP2/TEF72)
Markus.Liebe at de.bosch.com
Wed Feb 17 09:08:22 CET 2010
Hello,
I am trying to throw an exception in a function of a Qobject based class, that was created by a factory in a Qscript.
Hmm.. Kind of difficult to explain, but I have added the code of an example below.
The problem that I face is:
This code does what I expect it to do on windows XP using Qt SDK 2010.01: It catches the exception.
On Linux however (OpenSuse 11.0, gcc 4.3.1, Qt 4.6.1) it does not catch the exception but terminates the process:
--------------------------------------
terminate called after throwing an instance of 'std::runtime_error'
what(): test
--------------------------------------
Now I am wondering if it is only accidently that I can catch the exception on windows and the correct way to do sth. like this would be another one.
Do you have any hints on how to do this right on both platforms?
Thanks,
Markus
The code:
//-main.cpp------------------------------
#include <stdexcept>
#include <QScriptEngine>
#include <QtCore>
class Foo : public QObject {
Q_OBJECT
public slots:
void doSth(){
qDebug() << "throwing exception";
throw std::runtime_error("test");
}
};
class FooFactory : public QObject {
Q_OBJECT
public slots:
QObject* getFoo(){
return new Foo();
}
};
int main(int argc, char** argv){
QCoreApplication app(argc, argv);
FooFactory* factory = new FooFactory();
QScriptEngine engine;
QScriptValue scriptFooFactory = engine.newQObject(reinterpret_cast<QObject*>(factory));
engine.globalObject().setProperty("ff",scriptFooFactory);
QString strStartMakro;
strStartMakro = QString("var test = ff.getFoo();test.doSth()");
try{
engine.evaluate(strStartMakro);
if (engine.hasUncaughtException()) {
QStringList bt = engine.uncaughtExceptionBacktrace();
QString message = engine.uncaughtException().toString();
qWarning(__FUNCTION__);
qWarning("%s\n%s", qPrintable(message), qPrintable(bt.join("\n")));
QString strError = "Problem in the current macro. Printing backtrace:";
qDebug() << strError;
qDebug() << message;
foreach(QString line, bt){
qDebug() << line;
}
}
}catch(std::exception& e){
qDebug() << "Exception handler:" ;
qDebug() << e.what();
}
return app.exec();
}
#include "main.moc"
More information about the Qt-interest-old
mailing list