PDA

View Full Version : Catching exceptions with Qt



The_Fallen
29th July 2010, 21:23
Hi,

in my Qt app I use exceptions for the error handling. Everything works fine, except for those exceptions that I forgot to catch directly. In those cases the app crashes and has a console output like this:


Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.

So I assume the problem is that the exception is going through some Qt code and something goes wrong there. Now I thought: Ok, then I'll just catch all exceptions at the top-most level, i.e. the main function, like this.


try {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
catch (...) {
qDebug() << "CRASH!";
}

Unfortunately this doesn't seem to make a difference, the app still crashes. What am I doing wrong?

Cheers,
fallen

squidge
29th July 2010, 22:27
If an inner exception handler catches the error, your top-level (outer) exception handler will not receive it unless the inner exception handler rethrows the exception.

But really, its more typical to be an assert or debug() message.

The_Fallen
29th July 2010, 23:10
Yes, but does Qt catch exceptions anywhere? I didn't think so, since Qt doesn't use exceptions for error handling... And if Qt does catch exceptions, it's quite freaky that it doesn't rethrow it and just crashes...

GreenScape
30th July 2010, 01:42
look at your code again:


try {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
catch (...) {
qDebug() << "CRASH!";
}

when execution leaves try block all local variables automatically destroyed, maybe that leads to another exception to be thrown. i think we all know what happens when exception is thrown while there is another unresolved exception... so, try this:


QApplication a(argc, argv);
MainWindow w;
w.show();
try {
return a.exec();
}
catch (...) {
qDebug() << "CRASH!";
}

The_Fallen
30th July 2010, 10:05
Thanks, but unfortunately that doesn't help...

Zlatomir
30th July 2010, 10:29
Disclaimer: I'm not good with exceptions :o but i think you will need (to exclude the main return from the try block) something like this:


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//...
int ret_val;
try {
ret_val = app.exec();
}
catch (...){
qDebug() << "Exception";
}
return ret_val;
}

But remember that Qt doesn't throw exceptions, you will catch only yours.

The_Fallen
30th July 2010, 19:39
Thanks, but it's still crashing.
But I think I'm getting closer to the problem, since now I'm getting a segmentation fault in QApplicationPrivate::notify_helper():


// deliver the event
bool consumed = receiver->event(e);
e->spont = false;
return consumed;

receiver is just not valid anymore here... But why is this code still executed, when an execution has been thrown?