我有一个简单的脚本:
i=1
while True:
try:
print i
except KeyboardInterrupt:
raise Exception("Ended by user.")
i = i+1当我中断它时,它会打印出来:

我怎么能让它只打印出最后一行"Exception: and by user.",而不是"Traceback...“语句。
发布于 2018-01-18 23:59:18
要模拟异常但不使用回溯,您只需打印一条消息并使用非零代码退出:
except KeyboardInterrupt:
print("Ended by user.")
sys.exit(1)https://stackoverflow.com/questions/48325328
复制相似问题