try:
print(name)
except Exception as e:
print(type(e),e
print("gtq")
gtqDict={"Name":"光头强","Age":34,"Gender":"男"
try:
print(gtqDict["Weight"])
except KeyError:
print("Weight不存在,继续走"
print(gtqDict["Age"])
print(gtqDict["Gender"])
https://www.jb51.net/article/164035.htm
这段代码展示了Python中如何进行异常处理。首先尝试打印变量name,如果出现Exception,则捕获异常并输出类型及错误信息,最后打印'gtq'。接着处理字典gtqDict,尝试访问'Weight'键,当发生KeyError时,提示'Weight不存在,继续走',然后正常打印'Age'和'Gender'的值。
593

被折叠的 条评论
为什么被折叠?



