Python捕获全局的KeyboardInterrupt异常的方法实现
当然,像下面这种情况。
你要是把所有代码像下面那样都放到 try, expythoncept 的情况,就当我什么也没说。
import time def main(): print('before ...') time.sleep(10) print('afte编程r ...') if __name__ == '__main__': try: main() except KeyboardInterrupt: print('\nKeyboardInterrupt ...') print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^C KeyboardInterrupt ... the end
一般情况下,程序运行过程当中要执行的代码量会比较大,一般用户执行 Ctrl + C 程序就报错 KeyboardInterrupt 停止了。
import time def main(): print('before ...') time.sleep(10) print('after ...'编程) if __name__ == '__main__': main() print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^CTraceback (most recent call last): File "test.py", line 11, in <module> main() File "test.py", line 6, in main time.sleep(10) KeyboardInterrupt
但是有时候,我们希望用户在 Ctrl + C 之后再继续执行一些清理操作。
import sys import time def suppress_keyboard_interrupt_message(): old_excepthook = sys.excepthook def new_hook(exctype, value, traceback): if exctype != KeyboardInterrupt: old_excepthook(exctype, value, traceback) else: print('\nKeyboardInterrupt ...www.devze.com') print('do somethinrAOxaUwg after Interrupt ...') sys.excepthook = new_hook def main(): print('before ...') time.sleep(10) print('after ...') if __name__ == '__main__': suppress_keyboard_interrupt_message() main() print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^C KeyboardInterrupt ... do something after Interrupt ...
由于 suppress_keyboard_interrupt_message 函数中的 new_hook 是自定义的,所以你也不一定局限于只处理某种异常,甚至对所有异常做统一处理也是可以的。
到此这篇关于Python捕获全局的KeyboardInterrupt异常的方法实现的文章就介绍到这了,更多相关Python KeyboardInterrupt异常内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论