我最近开始使用 python 。当我遇到这种行为时,我正在处理键盘中断
import signal,sys
def handleInt(sign,no):
print "interrupted"
signal.signal(signal.SIGINT,handleInt) # exception raised is IOError
try:
sys.stdin.read(1)
except IOError:
print "io interrupt"
但是如果我将信号处理更改为在 try-except 之后
import signal,sys
def handleInt(sign,no):
print "interrupted"
try:
sys.stdin.read(1)
except KeyboardInterrupt:
print "keyboard interrupt"
signal.signal(signal.SIGINT,handleInt) # exception raised is KeyboardInterrupt
当我按 ctrl+c 时,两种情况下的异常存在差异。那么为什么会出现这种行为?