I want to catch KeyboardInterrupt
globally, and deal with it nicely. I don't want to encase my entire script in a huge try/except statement. Is there any way to do this?
You could change sys.excepthook
if you really don't want to use a try/except
.
import sys
def my_except_hook(exctype, value, traceback):
if exctype == KeyboardInterrupt:
print("Handler code goes here")
else:
sys.__excepthook__(exctype, value, traceback)
sys.excepthook = my_except_hook
Docs: