
Originally Posted by
mcostalba
A()
|---> try
|---> emit mySignal --> slotMySignal()
| |-----> throw X
|---> catch(x)
| |---> we will NOT catch X
Consider this little program:
#include <qobject.h>
{
Q_OBJECT
public:
void foo()
{
try {
emit someSignal();
}
catch( int i ) {
qWarning( "exception caught i=%d", i );
}
}
signals:
void someSignal();
};
{
Q_OBJECT
public slots:
void someSlot()
{
throw 1234;
}
};
int main()
{
A a;
B b;
QObject::connect( &a,
SIGNAL( someSignal
() ),
&b,
SLOT( someSlot
() ) );
a.foo();
return 0;
}
#include "main.moc"
#include <qobject.h>
class A : public QObject
{
Q_OBJECT
public:
void foo()
{
try {
emit someSignal();
}
catch( int i ) {
qWarning( "exception caught i=%d", i );
}
}
signals:
void someSignal();
};
class B : public QObject
{
Q_OBJECT
public slots:
void someSlot()
{
throw 1234;
}
};
int main()
{
A a;
B b;
QObject::connect( &a, SIGNAL( someSignal() ), &b, SLOT( someSlot() ) );
a.foo();
return 0;
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Output:
$ ./except
exception caught i=1234

Originally Posted by
mcostalba
the _real_ problem is that also PE is a barrier to exceptions propagation.
I wonder if event queue would survive this. An exception would abort QApplication::processEvents() right in the middle.
Anyway, you are right that there is a problem with exceptions in Qt-based applications.
IMO the best way is to divide the application into two parts. One that implements the functionality (model, core, engine or whatever) and second one that is responsible for the GUI. Of course you should avoid exceptions in the latter.
The code might look like this:
void SomeWidget::someSlot()
{
try {
core::SomeObject::instance().doSomething();
}
catch( const core::Exception& e ) {
MessageBox::exception( e );
}
}
void SomeWidget::someSlot()
{
try {
core::SomeObject::instance().doSomething();
}
catch( const core::Exception& e ) {
MessageBox::exception( e );
}
}
To copy to clipboard, switch view to plain text mode
Where MessageBox is a class that represents a message box informing user about a failure and core::Exception is a base class for all exceptions that can be passed to GUI part.
Bookmarks