0

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.

sammosummo
  • 495
  • 1
  • 7
  • 17
  • maybe https://stackoverflow.com/questions/18740884/preventing-pyqt-to-silence-exceptions-occurring-in-slots – eyllanesc Jan 11 '19 at 15:51
  • For me, overriding `sys.excepthook` gives exactly the same behaviour for both examples. Please provide a proper mcve that shows why you "can't capture any of your exceptions properly" (whatever that means) when using an excepthook. (And note that this is all [documented behaviour](http://pyqt.sourceforge.net/Docs/PyQt5/incompatibilities.html#unhandled-python-exceptions), so there is nothing unexpected happening here). – ekhumoro Jan 11 '19 at 18:03
  • @ekhumoro Please ignore the reference to `sys.excepthook`. It was confusing and actually not directly related to the question. – sammosummo Jan 11 '19 at 20:59
  • I did read the link, I just didn't fully understand it. Thank you for the clarification. – sammosummo Jan 12 '19 at 14:36

0 Answers0