As detailed in the docs, all exceptions can be caught by monkey patching sys.excepthook, while __excepthook__ contains the original value. However, when used in some Qt events, altough the error is caught, the app doesn't crash.
In the example below, the app crash when raise is called in __init__, but not in keyPressEvent. The workaround is to call sys.exit(1) after __excepthook__.
But why is it needed? Anyone has an idea?
In the example below, the app crash when raise is called in __init__, but not in keyPressEvent. The workaround is to call sys.exit(1) after __excepthook__.
But why is it needed? Anyone has an idea?
#!/usr/bin/python3 import sys from PyQt5 import QtWidgets class Text(QtWidgets.QPlainTextEdit): def __init__(self): super().__init__() # raise NotImplementedError # Crash (expected behavior) def keyPressEvent(self, event): raise NotImplementedError # Does not crash (unexpected) super().keyPressEvent(event) class Main(QtWidgets.QWidget): def __init__(self, parent): super().__init__() layout = QtWidgets.QVBoxLayout() layout.insertWidget(0, Text()) self.setLayout(layout) self.show() def _exception(type_, value, tb): print(f"Caught: {type_} {value}".rstrip()) sys.__excepthook__(type_, value, tb) # sys.exit(1) sys.excepthook = _exception if __name__ == '__main__': app = QtWidgets.QApplication([]) gui = Main(app) app.exec()