Exceptions are handled in a strange way during Qt apps created from Python (creating using PyQt5 here, but I noticed similar behavior with PySide and PyQt4). Please consider the following script. It is perhaps a little too verbose, but I wanted to create a semi-realistic example, where the app, the main window, and the central widget were all created separately (perhaps by separate Python modules as in a realistic app).
from sys import argv, exit
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout, \
QPushButton
def main():
print("running the main Python function")
run_app()
def run_app():
print("intialising the app")
app = QApplication(argv)
ex = MainWindow()
exit(app.exec_())
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
print("initialising the main window")
gui = GUIWidget(self)
self.setCentralWidget(gui)
self.show()
class GUIWidget(QWidget):
def __init__(self, parent=None):
super(GUIWidget, self).__init__(parent)
print("initialising the gui")
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
self.button = QPushButton("Push me to produce an error")
self.button.clicked.connect(self.raise_an_expection)
self.vbox.addWidget(self.button)
# raise Exception # nicely handled exception
def raise_an_expection(self):
raise Exception # poorly handled exception
if __name__ == '__main__':
main()
With the final line of GUIWidget.__init__()
uncommented, Python raises an exception and Process finished with exit code 1
, as expected.
With this commented, an app with a button is created . Pushing the button raises an exception, as expected, but also Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
, which I don't understand. On my mac, this also causes a Python quit unexpectedly
dialog to appear.