【发布时间】:2016-05-08 04:16:27
【问题描述】:
此 Python 代码块在 student [3] 处生成错误。如何在except 中立即显示错误信息而不是打印之前的值?
try:
student = ['bob','rob','mob']
print (student )
print (student [0])
print (student [1])
print (student [2])
print (student [3])
except:
print("error")
电流输出:
['bob','rob','mob']
bob
rob
mob
error
Process finished with exit code 0
如何避免在出现错误之前打印?
【问题讨论】:
-
请不要添加无关标签
-
@TimCastelijns kk ..使用那个 ide :)
-
你想展示的实际信息是什么?
-
@thefourtheye ,python 中的新功能,实际上我需要像在.net 中一样在尝试中出现错误时立即从尝试中跳出来 .. 并在 catch 或 except 部分显示消息
-
@utility 这正是 Python 所做的。它打印
mob,因为它可以在遇到student [2]时执行。
标签: python exception exception-handling try-catch indexoutofboundsexception