【发布时间】:2021-01-24 10:24:37
【问题描述】:
我现在正在学习如何在 python 中处理多个错误。在使用try-except 时,我想打印try 中的每个错误。 try 中有两个错误,但首先出现了索引错误,因此程序无法打印关于 ZeroDivisionError 的消息。如何同时打印IndexErrormessage 和ZeroDivisionErrormessage?
下面是我写的代码。
try:
a = [1,2]
print(a[3])
4/0
except ZeroDivisionError as e:
print(e)
except IndexError as e:
print(e)
【问题讨论】:
标签: python exception try-except