#!/usr/bin/python
try:
text = input('Enter something-->')
except EOFError:
print('Why did you do an EOF on me?')
except KeyboardInterrupt:
print('You cancelled the operation.')
else:
print('You entered {0} haha'.format(text))
结果:
ang@ubuntu:a_byte_python->./6try_catch.py
Enter something-->^CYou cancelled the operation.
ang@ubuntu:a_byte_python->./6try_catch.py
Enter something-->0
You entered 0 haha