Why can't I handle a KeyboardInterrupt in python?

前端 未结 6 2063
不知归路
不知归路 2020-12-02 13:13

I\'m writing python 2.6.6 code on windows that looks like this:

try:
    dostuff()
except KeyboardInterrupt:
    print \"Interrupted!\"
except:
    print \"S         


        
6条回答
  •  执笔经年
    2020-12-02 14:02

    Here's a guess about what's happening:

    • pressing Ctrl-C breaks the "print" statement (for whatever reason... bug? Win32 limitation?)
    • pressing Ctrl-C also throws the first KeyboardInterrupt, in dostuff()
    • The exception handler runs and tries to print "Interrupted", but the "print" statement is broken and throws another KeyboardInterrupt.
    • The finally clause runs and tries to print "cleaning up....", but the "print" statement is broken and throws yet another KeyboardInterrupt.

提交回复
热议问题