Results 1 to 7 of 7

Thread: Catching exceptions with Qt

  1. #1
    Join Date
    Mar 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Catching exceptions with Qt

    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.
    Qt Code:
    1. try {
    2. QApplication a(argc, argv);
    3. MainWindow w;
    4. w.show();
    5. return a.exec();
    6. }
    7. catch (...) {
    8. qDebug() << "CRASH!";
    9. }
    To copy to clipboard, switch view to plain text mode 
    Unfortunately this doesn't seem to make a difference, the app still crashes. What am I doing wrong?

    Cheers,
    fallen

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Catching exceptions with Qt

    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.

  3. #3
    Join Date
    Mar 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Catching exceptions with Qt

    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...

  4. #4
    Join Date
    Jul 2010
    Posts
    53
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Catching exceptions with Qt

    look at your code again:

    Qt Code:
    1. try {
    2. QApplication a(argc, argv);
    3. MainWindow w;
    4. w.show();
    5. return a.exec();
    6. }
    7. catch (...) {
    8. qDebug() << "CRASH!";
    9. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QApplication a(argc, argv);
    2. MainWindow w;
    3. w.show();
    4. try {
    5. return a.exec();
    6. }
    7. catch (...) {
    8. qDebug() << "CRASH!";
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by GreenScape; 30th July 2010 at 01:49.

  5. #5
    Join Date
    Mar 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Catching exceptions with Qt

    Thanks, but unfortunately that doesn't help...

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Catching exceptions with Qt

    Disclaimer: I'm not good with exceptions but i think you will need (to exclude the main return from the try block) something like this:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. //...
    5. int ret_val;
    6. try {
    7. ret_val = app.exec();
    8. }
    9. catch (...){
    10. qDebug() << "Exception";
    11. }
    12. return ret_val;
    13. }
    To copy to clipboard, switch view to plain text mode 
    But remember that Qt doesn't throw exceptions, you will catch only yours.

  7. #7
    Join Date
    Mar 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Catching exceptions with Qt

    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():
    Qt Code:
    1. // deliver the event
    2. bool consumed = receiver->event(e);
    3. e->spont = false;
    4. return consumed;
    To copy to clipboard, switch view to plain text mode 
    receiver is just not valid anymore here... But why is this code still executed, when an execution has been thrown?

Similar Threads

  1. emitting and catching signals
    By srohit24 in forum Qt Programming
    Replies: 12
    Last Post: 4th August 2009, 18:32
  2. Advice catching dropEvent
    By DirtyBrush in forum Qt Programming
    Replies: 5
    Last Post: 15th July 2009, 16:43
  3. exception catching speed
    By stefan in forum General Programming
    Replies: 10
    Last Post: 12th November 2008, 19:58
  4. Qt - catching interrupts
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2008, 14:17
  5. Catching X Events
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2006, 12:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.