Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log uncaught exceptions of a QApplication?

Tags:

qt4

pyqt4

Where should i use an except block in order to log exceptions of a QApplication?

This doesn't seem to work:

app = QtGui.QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
try:
    eventLoop = app.exec_()
except Exception, e:
    log.exception(str(e))

as the exception won't even reach that block.

like image 238
iTayb Avatar asked May 19 '12 13:05

iTayb


People also ask

What should qapplication notify() return when it catches an exception?

So, what should QApplication::notify () return when it catches an exception? Sends event to receiver: receiver->event (event). Returns the value that is returned from the receiver's event handler.

How to add an uncaught exception handler in an application class?

Essentially what you need to do is add an uncaught exception handler in your Application class that will log the errors before the app has to terminate. First, create this exception handler: And finally, register this crash handler in your application class:

How do I catch unhandled exceptions in my application?

The .NET Framework provides a couple of events that you can use to catch unhandled exceptions. You only need to register for these events once in your code when your application starts up. For ASP.NET, you would do this in the Startup class or Global.asax. For Windows applications, it could be the first couple lines of code in the Main () method.

What is uncaught exceptions in Java?

Uncaught Exceptions in Java PreviousNext In java, assume that, if we do not handle the exceptions in a program. In this case, when an exception occurs in a particular function, then Java prints a exception message with the help of uncaught exception handler.


1 Answers

Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.

Overwrite the function bool QApplication::notify(QObject * receiver, QEvent *event) so that it catches all thrown exceptions.

You can implement like this.

virtual bool notify(QObject * receiver, QEvent * event) 
{
     try 
     {
     return QApplication::notify(receiver, event);
     } 
     catch(std::exception& e) 
     {
      qDebug() << "Exception thrown:" << e.what();
     }
}
like image 153
ScarCode Avatar answered Oct 21 '22 19:10

ScarCode