[Interest] Recommended exception handling

william.crocker at analog.com william.crocker at analog.com
Mon Mar 11 21:55:15 CET 2019


>
> Hello,
> Is there a current recommended way of catching exceptions before they unwind
> through the event loop? I want this for "debug purposes" only. I have few
> methods that throw and I (think I) catch (all of) my exceptions before they
> reach the event loop, but I wanted to be sure I hadn't forgotten something
> somewhere. Shouldn't happen anyway, but I was wondering if there's (or if
> there's going to be) a replacement for QCoreApplication::notify.
>

I wrote and installed my own notify function.
My app only ever throws QStrings.

Something like...

class MyApplication : public QApplication {
     Q_OBJECT
     typedef QApplication BaseClass;
   public:
     MyApplication(int&,char**);
    ~MyApplication();
     bool notify(QObject*,QEvent*);	// This is virtual.
};

bool
MyApplication::notify( QObject *receiver, QEvent *event ) {
     try {
         return BaseClass::notify(receiver,event);
     } catch( QString msg ) {				// What I throw.
         msg = msg.trimmed(); msg += " Operation aborted.";
         LogEvent( EventLevel_Critical, msg );
         return false;
    } catch( std::bad_alloc & ) {
         LogEvent( EventLevel_SystemError, "Could not allocate any more memory...");
         return false;
     } catch( std::exception &e ) {
         LogEvent( EventLevel_SystemError, QString("Standard exception: 
%1.").arg(e.what()) );
         return false;
     } catch( ... ) {
         LogEvent( EventLevel_SystemError, "MyApplication::notify: Caught an 
unexpected exception." );
         return false;
     }
}

> Thanks in advance!
>
> Kind regards,
> Konstantin.
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>



More information about the Interest mailing list