简体   繁体   English

如何打印程序退出/程序终止/退出错误的原因?

[英]How to print the reason for program exit / program termination / exit errors?

I managed to catch the termination with:我设法通过以下方式赶上终止:

atexit.register(exitHandler)

But how to print out what happened on that point?但是如何打印出当时发生的事情呢? I want to see if the program aborts due to an error, due to Crtl-C or normal stopping...我想看看程序是否由于错误而中止,由于 Crtl-C 或正常停止...

It's not the complete solution - but you could wrap code in try except这不是完整的解决方案 - 但您可以在 try 中包装代码,除了

try:
    YOUR CODE HERE

except Exception as e:
    print(e)

You should catch KeyboardInterrupt for Ctrl-CEg:您应该为 Ctrl-CEg 捕获KeyboardInterrupt

import sys


try:
    # your code
except KeyboardInterrupt:
    sys.exit('Abort by user interrupt')
except Exception as exc:
    sys.exit(f'Abort on error: {exc}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM