Why can't I handle a KeyboardInterrupt in python?

前端 未结 6 2102
不知归路
不知归路 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 13:51

    def foo():
        try:
            x=0
            while 1:
                x+=1
                print (x)
        except KeyboardInterrupt:
           print ("interrupted!!")
    foo()
    

    That works fine.

提交回复
热议问题