A
A
Alexander Rublev2017-01-14 14:07:22
Python
Alexander Rublev, 2017-01-14 14:07:22

How to catch a program crash error?

Hello!
I am writing a program in PyQt. The program is big enough (For me).
When testing, I constantly catch and correct errors through the debugger! (But I can't check everything)
But when I give the program to the workers they will debug. And if an error occurs, the program simply closes!
Question: How to show or record where the error occurred and why?
P.S. My program starts like this:

app = QApplication(sys.argv)
main = form.main_window.MainWindow()
sys.exit(app.exec_())

In my case, the solution is: (Shows exactly where the error occurred and when + writes to the file)
import sys, traceback
from datetime import datetime


def my_excepthook(t, v, tb):
    with open('1.txt', 'a') as f:
        f.write("\n---===Error===---\n")
        f.write("Time = %s\n" % datetime.now().strftime("%d.%m.%Y"))
        traceback.print_exception(t, v, tb, file=f)


sys.excepthook = my_excepthook

app = QApplication(sys.argv)
main = form.main_window.MainWindow()
sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-01-14
@Meller008

https://docs.python.org/3/library/sys.html#sys.exc...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question