异常处理
Try Except 形式
try:
a = 1 / 0 # trigger the exception on purpose
except ZeroDivisionError:
print("cannot divide by zero")
多个 Except
try:
a = int("NaN") # trigger the ValueError on purpose
a / 0 # trigger the ZeroDivisionError on purpose
except ZeroDivisionError:
print("cannot divide by zero")
except ValueError:
print("invalid value for integer parsing")
Try Else 形式
try:
a = int("5")
a / 10
except ZeroDivisionError:
print("cannot divide by zero")
except ValueError:
print("invalid value for integer parsing")
else:
print("everything is ok")
代码挑战
修改编辑器中代码,触发指定的异常。
Loading...
> 此处输出代码运行结果