Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python handling any exception gives error KeyboardInterrupt

Here's the code:

try:
    input() # I hit Ctrl+C
except Exception as e:
    print(str(e))

Here's the traceback:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\test.py", line 2, in <module>
    input()
KeyboardInterrupt
like image 502
Joe Doe Avatar asked Oct 21 '25 15:10

Joe Doe


1 Answers

please, be explicit, when you want to catch KeyboardInterrupt:

try:
    input() # I hit Ctrl+C
except Exception as e:
    print(str(e))
except KeyboardInterrupt :
    print 'ok, ok, terminating now...'
like image 53
lenik Avatar answered Oct 23 '25 03:10

lenik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!