1

I'm using PyQt5 as a GUI for my python app. Sometimes the code crashes with these types of errors:

pyqt5 error Image

Is there a way to log these types of crashes with explanation (like error exception).

Edit:
found an easy way.
just run via cli
>> py filename.py
works like magic. (or if using pycharm, debug mode)

yaron
  • 73
  • 5

1 Answers1

0

The general method for error catching is this:

try:
    # try something that does not work
    a*5
except Exception as e:
    print(e)

the output would look like this (for the example error):

name 'a' is not defined

Here is a link: https://docs.python.org/3/tutorial/errors.html

However, you might need to do other debugging, logging and printing to terminal to find the error.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • This won't necessarily work in the OP's case. If Qt gets upset somewhere in its C++ backend, it prints the reason to std error and calls abort, it doesn't go via python exceptions. – stevecu Aug 01 '22 at 20:09
  • @stevecu, what would be the approach in respect of a case where the C++ backend was upset as you have pointed out ? – D.L Aug 02 '22 at 13:09
  • If you call `QtCore.qInstallMessageHandler`, see for example [here](https://forum.qt.io/topic/84022/code-for-qinstallmessagehandler) right at the start of your Qt usage, you can catch fatal/critical errors. Qt will still raise `abort` but at least you can log the reason first. However, if you access Qt from the wrong thread, I think it's possible to cause a genuine fault-type crash, in which case this handler won't be called. – stevecu Aug 04 '22 at 19:55
  • So basically outside of the standard `try-except` statement. This is the next best alternative for any C++ back end... – D.L Aug 05 '22 at 07:22