try:
except Exception:
例子:
class Dog(object):
def __init__(self, name):
self.name = name
def eat(self, food):
print("%s is eating... %s" %(self.name, food))
d = Dog('liuhuayang')
try:
print(getattr(d, "age"))
except Exception:
print("extions")
例子2:
data = {"name":"liaodalin"}
try:
age = data["age"]
except KeyError:
print("there is no key for age") # 打印
# 給鍵制作
data = {"name":"liaodalin"}
try:
age = data["age"]
except KeyError as e:
print("there is no key for age ", e)
else的作用:
try:
xxx
except Exception:
print("有錯(cuò)")
else:
print("一切正常允坚!")
finally:
pring("不管有沒有錯(cuò),都執(zhí)行")