更新时间:2022-03-17 05:37:41
我也处理过同样的问题,答案是双重的:
I have dealt with the same problem, and the answer is twofold:
要捕获异常,您需要覆盖 sys 异常处理程序:
To catch the exceptions, you need to overwrite the sys exception handler:
# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
然后在您的执行代码中,将其包装在 try/catch 中.
Then in your execution code, wrap it in a try/catch.
try:
sys.exit(app.exec_())
except:
print("Exiting")