Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt/C++ event loop exception handling

I am having an application heavily based on QT and on a lot of third party libs. These happen to throw some exceptions in several cases.

In a native Qt App this causes the application to abort or terminate. Often the main data model is still intact as I am keeping it in pure Qt with no external data.

So I am thinking that I could also just recover by telling the user that there has occurred an error in this an that process and he should save now or even decide to continue working on the main model.

Currently the program just silently exits without even telling a story.

like image 329
Georg Avatar asked Dec 22 '22 03:12

Georg


1 Answers

Sometimes it's really hard to catch all exception. If one exception accidently slips through, the following helps a lot. Inherit from QApplication and override the notify() function in the following way

bool MyApplication::notify( 
QObject * receiver, 
QEvent *  event ) 
{
    try 
    {
        return QApplication::notify(receiver, event);
    }
    catch(...)
    {
        assert( !"Oops. Forgot to catch exception?" );

        // may be handle exception here ...
    }

    return false;
}

Then replace the QApplication in your main() function by your custom class. All events and slots are issued through this function, so that all exceptions can be caught and your application becomes stable.

like image 172
Ralph Tandetzky Avatar answered Jan 18 '23 01:01

Ralph Tandetzky